【发布时间】:2019-08-22 12:54:47
【问题描述】:
我想并行化这段代码:
import numpy as np
import requests
# the 'in memory file' I use at the moment
bytesIO = io.BytesIO()
data = np.random.randint(0, 256, (30, 30))
np.savez(bytesIO, data=data)
# go to the beginning of the buffer again
bytesIO.seek(0)
# upload the file to a different server
requests.post("http://example.org/, files={'file': bytesIO},
data={'filename': 'My_File'})
在这里
- 数据已生成,
- 数据在请求中发送。
我希望数据在被序列化到缓冲区时被传输。可能使用 2 个线程,通过 queue 连接。
对于传输,请求支持streaming uploads。
但是np.savez 和requests 都希望有一个类似文件的对象来读取/写入。队列不是文件式的,BytesIO 不是线程安全的。
解决这个问题的最佳方法是什么?
【问题讨论】:
标签: python multithreading io python-multithreading