线程或异步收集?
Threads or asyncio gather?
执行并发 i/o 操作的最佳方法是什么?
- 线程或
- 异步
会有文件列表。
我打开文件并使用 .txt
文件生成图表并将其存储在磁盘上。
我尝试过使用线程,但它很耗时,有时它不会为某些文件生成图表。
还有其他方法吗?
我尝试使用下面的代码在 load_instantel_ascii
函数上使用异步,但它给出了异常
for fl in self.finallist:
k = randint(0, 9)
try:
task2.append( * [load_instantel_ascii(fleName = fl, columns = None,
out = self.outdir,
separator = ',')])
except:
print("Error on Graph Generation")
event_loop.run_until_complete(asyncio.gather(yl1
for kl1 in task2)
)
如果我理解的一切正确并且您想要异步文件 I/O,那么 asyncio
本身 doesn't support it out of the box. In the end all asyncio-related stuff that provides async file I/O 使用线程池来完成它。
但这可能并不意味着您不应该使用 asyncio
:这个库作为一种首先编写异步代码的方式很酷,即使它包装在线程之上。我会尝试 aiofiles.
执行并发 i/o 操作的最佳方法是什么?
- 线程或
- 异步
会有文件列表。
我打开文件并使用 .txt
文件生成图表并将其存储在磁盘上。
我尝试过使用线程,但它很耗时,有时它不会为某些文件生成图表。
还有其他方法吗?
我尝试使用下面的代码在 load_instantel_ascii
函数上使用异步,但它给出了异常
for fl in self.finallist:
k = randint(0, 9)
try:
task2.append( * [load_instantel_ascii(fleName = fl, columns = None,
out = self.outdir,
separator = ',')])
except:
print("Error on Graph Generation")
event_loop.run_until_complete(asyncio.gather(yl1
for kl1 in task2)
)
如果我理解的一切正确并且您想要异步文件 I/O,那么 asyncio
本身 doesn't support it out of the box. In the end all asyncio-related stuff that provides async file I/O 使用线程池来完成它。
但这可能并不意味着您不应该使用 asyncio
:这个库作为一种首先编写异步代码的方式很酷,即使它包装在线程之上。我会尝试 aiofiles.