简体   繁体   English

在循环的每次迭代中运行一个函数作为 python 中的一个新进程

[英]Running a function in each iteration of a loop as a new process in python

I have this:我有这个:

from multiprocessing import Pool

pool = Pool(processes=4)

def createResults(uniqPath):
    *(there is some code here that populates a list - among other things)*

for uniqPath in uniqPaths:
    pool.map(createResults, uniqPath)

pool.close()
pool.join()

I don't know if it's possible, but can I run the createResults function that gets called in that loop as a new process for each iteration?我不知道这是否可能,但是我可以运行在该循环中调用的 createResults 函数作为每次迭代的新进程吗?

I'm populating a list using a 4 million line file and it's taking 24+ hours to run.我正在使用一个 400 万行的文件填充一个列表,运行需要 24 多个小时。 (Obviously the code above does not work) (显然上面的代码不起作用)

Thanks!谢谢!

Instead of:代替:

for uniqPath in uniqPaths:
    pool.map(createResults, uniqPath)

Do this:做这个:

pool.map(createResults, uniqPaths)

You must use map on the iterable itself in order to run in a concurrent fashion.您必须在迭代本身上使用 map 才能以并发方式运行。
Keep in mind though - Populating a list means the list won't be shared between the processes, and if it does using Array() , make sure it's process-safe.但请记住 - 填充列表意味着该列表不会在进程之间共享,如果确实使用了Array() ,请确保它是进程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM