首页 > 解决方案 > Python多处理:在并行执行之前和之后串行执行代码

问题描述

新手在这里:我正在尝试串行执行一些代码,然后创建一个线程池并并行执行一些代码。并行执行完成后,我想串行执行更多代码。

例如...

import time
from multiprocessing import Pool

print("I only want to print this statement once")



def worker(i):
    """worker function"""
    now = time.time()
    time.sleep(i)
    then = time.time()
    print(now, then)

if __name__ == '__main__':
    with Pool(3) as p:
        p.map(worker, [1, 1, 1])
        p.close()

print("Only print this once as well")

我希望这个返回...

I only want to print this statement once
1533511478.0619314 1533511479.0620182
1533511478.0789354 1533511479.0791905
1533511478.0979397 1533511479.098235
Only print this once as well

然而它返回的是这样的:

I only want to print this statement once
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
I only want to print this statement once
Only print this once as well
1533511478.0619314 1533511479.0620182
1533511478.0789354 1533511479.0791905
1533511478.0979397 1533511479.098235
Only print this once as well

因此,似乎每个池都额外运行了一次打印语句。

任何帮助,将不胜感激!

标签: pythonmultiprocessingpython-multiprocessingpool

解决方案


根据观察到的行为,我假设您使用的是 NT/Windows 操作系统。

您看到所有这些打印的原因是因为在 Windows 上使用了spawn启动策略。当“生成”一个新进程时,会启动一个新的 Python 解释器并接收模块和它应该执行的函数。当新解释器导入模块时,会执行顶层print函数。因此重复打印。

只需将这些打印语句移动到其中__main__,您就不会再看到它们了。


推荐阅读