如果一个进程是令人尴尬的并行的,并且线程之间不需要通信,那么我将只使用一个进程池上下文管理器你有很多争论,这可能很棘手,但有很多解决方法来传递争论我有时会使用嵌套函数或全局变量来转换成一个变量。否则,您总是可以发送一个列表或类似的列表。
from multiprocessing.dummy import Pool as ProcessPool # Only use this if you're CPU bound. If you're IO bound use a threadpool rather
my_zip_files = [
("arg1", "arg2", "arg3"), # Whatever the arguments are. Just roll them into a tuple and store all the tuples in a list.
("arg12", "arg22", "arg23")
]
def do_something(arg):
arg1 = arg[0] # recover the individual arguments. Can use python's * syntax as well.
arg2 = arg[1]
arg3 = arg[2]
result = _do_something(arg1, arg2, arg3)
return result
def _do_something(arg1, arg2, arg3):
<whatever logic>
with ProcessPool(4) as pool:
result = pool.map(do_something, my_zip_files)
print(result) # And now the order of the files should be the same as the order you put them in. I've run some checks and order seems to be preserved but maybe check the docs to be sure.