[英]Writing files with a pool of workers
I have to write some files that are quite big, I'd like to do this in a separate thread (use processor and HDD at the same time) and I'd like to use a pool of workers of size 4 (because there are other operations besides writing, I guesstimate 4 is enough to use the HDD properly without clogging it). 我必须编写一些很大的文件,我想在一个单独的线程中执行此操作(同时使用处理器和HDD),并且我想使用大小为4的工作池(因为有除了写以外的其他操作,我猜想4足以正确使用HDD而不会阻塞它)。
This is the code I have (it does nothing): 这是我的代码(什么都不做):
import os, os.path, multiprocessing
def asyncFileMake(e):
with open('files/file%d.txt'%e, 'w') as f:
print(os.path.dirname(os.path.abspath(os.getcwd())))
f.write("hi")
def writefile(e, pool):
pool.apply_async(asyncFileMake, [e])
if __name__ == "__main__":
if not os.path.exists('files'):
os.makedirs('files')
with multiprocessing.Pool(processes=4) as pool:
for e in range(10):
writefile(e, pool)
Here's a version which is inspired by your original code snippet. 这是一个受您原始代码段启发的版本。 I think the most significant change is the way the function is passed to the pool (
map
instead of apply_async
, more on that later): 我认为最重要的变化是将函数传递给池的方式(用
map
代替apply_async
,稍后再介绍):
import os
import multiprocessing
def create_file(e):
with open('files/file%d.txt'%e, 'w') as f:
print f.name
f.write("hi")
if __name__ == '__main__':
if not os.path.exists('files'):
os.makedirs('files')
pool = multiprocessing.Pool(processes=4)
pool.map(create_file, range(10))
The problem with your original implementation is, that pool.apply_async
returns a AsyncResult
on which you need to call get
so that the actual execution gets triggered. 原始实现的问题是,
pool.apply_async
返回一个AsyncResult
,您需要在其上调用get
以便触发实际执行。 I suggest you take another close look at the documentation of multiprocessing . 我建议您再仔细看一看多处理文档 。 Especially this part about pool workers .
特别是关于泳池工人的这一部分 。 Hope that helps.
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.