方法内部的多处理回调不起作用

时间:2019-05-20 20:14:14

标签: python python-3.x multiprocessing python-multiprocessing multiprocess

我正在尝试在方法中运行多重处理。但是,回调似乎无法正常运行。

这是我需要实现的一个示例。

from multiprocessing import Pool
from time import sleep
import random

def sum(task, a, b): 
    sleepTime = random.randint(1, 4)
    print(task, " requires ", sleepTime, " seconds to finish")
    sleep(sleepTime)
    return a+b 

def printResult(result):
    print(result)

myPool = Pool(5)

result1 = myPool.apply_async(sum, args=("task1", 10, 20,), callback = printResult)
result2 = myPool.apply_async(sum, args=("task2", 20, 30,), callback = printResult)
result3 = myPool.apply_async(sum, args=("task3", 30, 40,), callback = printResult)
result4 = myPool.apply_async(sum, args=("task4", 40, 50,), callback = printResult)
result5 = myPool.apply_async(sum, args=("task5", 50, 60,), callback = printResult)

print("Submitted tasks to pool")

myPool.close()
myPool.join()

返回正常效果

Submitted tasks to pool
task1  requires  3  seconds to finish
task2  requires  4  seconds to finish
task3  requires  1  seconds to finish
task4  requires  4  seconds to finish
task5  requires  1  seconds to finish
110
70
30
50
90

但是,当我在一个方法中包装多处理并调用该方法时,它似乎无法按预期运行。

例如

def run_mp():
    from multiprocessing import Pool
    from time import sleep
    import random

    def sum(task, a, b): 
        sleepTime = random.randint(1, 4)
        print(task, " requires ", sleepTime, " seconds to finish")
        sleep(sleepTime)
        return a+b 

    def printResult(result):
        print(result)

    myPool = Pool(5)

    result1 = myPool.apply_async(sum, args=("task1", 10, 20,), callback = printResult)
    result2 = myPool.apply_async(sum, args=("task2", 20, 30,), callback = printResult)
    result3 = myPool.apply_async(sum, args=("task3", 30, 40,), callback = printResult)
    result4 = myPool.apply_async(sum, args=("task4", 40, 50,), callback = printResult)
    result5 = myPool.apply_async(sum, args=("task5", 50, 60,), callback = printResult)

    print("Submitted tasks to pool")

    myPool.close()
    myPool.join()

调用run_mp()时,它仅返回以下内容。

Submitted tasks to pool

这不仅是屏幕上标准输出的问题。在我要处理的实际问题中,似乎没有正确调用回调函数。

1 个答案:

答案 0 :(得分:1)

简单的答案。用multiprocessing腌制类方法是不可能的,或者至少很容易。有一个multiprocessing的分支,称为multiprocess(我是作者),它使用dill序列化程序...并完全启用您要查找的行为。

只需将from multiprocessing替换为from multiprocess,您的代码就会按预期运行。