python ProcessPoolExecutor 在运行时不起作用

标签 python multiprocessing

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/

相关文章:

python - POST 请求在使用 Plivo 的 Django IVR 中不起作用

python - 使用 python flask 显示 opencv 图像

python - 如何替换数据框中某个字符的所有实例?

python - 在 Windows 上隐藏 python 进程和线程输出

python - Pathos 处理池递归限制

python - 多处理问题 [pyqt, py2exe]

python - 更改 Seaborn 图中的轴

Python Elasticsearch,RequestError : TransportError(400, u'search_phase_execution_exception') 间歇性发生。为什么?

windows - ansi 终端和 native io 管理器不起作用

python - 无法让多处理工作