gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:37 25 4
gpt4 key购买 nike

我有这个:

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()

我不知道这是否可能,但是我可以运行在该循环中调用的 createResults 函数作为每次迭代的新进程吗?

我正在使用 400 万行文件填充列表,并且运行时间超过 24 小时。 (显然上面的代码不起作用)

谢谢!

最佳答案

而不是:

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

这样做:

pool.map(createResults, uniqPaths)

您必须在可迭代本身上使用映射才能以并发方式运行。
但请记住 - 填充列表意味着该列表不会在进程之间共享,如果确实使用 Array() ,确保其过程安全。

关于python - 在循环的每次迭代中将函数作为 python 中的新进程运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36090370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com