gpt4 book ai didi

python ProcessPoolExecutor 在运行时不起作用

转载 作者:行者123 更新时间:2023-12-05 01:15:05 24 4
gpt4 key购买 nike

python ProcessPoolExecutor 在命令行中工作,但添加到函数后不运行

它是这样工作的

from concurrent import futures

def multi_process(func, paras, threads):
with futures.ProcessPoolExecutor(max_workers=threads) as pool:
res = pool.map(func, paras, chunksize=threads)
return list(res)
p = multi_process(func,paras,threads)

但根本不工作,如下所示

def upper(paras,threads):
def func:
some func
def multi_process(func, paras, threads):
with futures.ProcessPoolExecutor(max_workers=threads) as pool:
res = pool.map(func, paras, chunksize=threads)
return list(res)
p = multi_process(func,paras,threads)
return p
p = upper(paras,threads)

没有警告或错误但很长时间没有任何响应。

最佳答案

你确实得到了一个错误。它的。

AttributeError: Can't pickle local object 'upper.<locals>.func'.

原因是多处理工作需要在全局级别定义函数。

要实现您想要的效果,您可以执行以下操作:

from concurrent import futures

# Has to be a global function
def func(para):
print(para)


def upper(paras,threads):
# This cannot be a local function.
#def func(para):
# print(para)
def multi_process(func, paras, threads):
with futures.ProcessPoolExecutor(max_workers=threads) as pool:
res = pool.map(func, paras, chunksize=threads)
return list(res)
p = multi_process(func, paras, threads)
return p

paras = [1, 2, 3]
threads = 3
p = upper(paras,threads)

关于python ProcessPoolExecutor 在运行时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56968666/

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