python - 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
因此,似乎每个池都额外运行了一次打印语句。
任何帮助,将不胜感激!
解决方案
根据观察到的行为,我假设您使用的是 NT/Windows 操作系统。
您看到所有这些打印的原因是因为在 Windows 上使用了spawn
启动策略。当“生成”一个新进程时,会启动一个新的 Python 解释器并接收模块和它应该执行的函数。当新解释器导入模块时,会执行顶层print
函数。因此重复打印。
只需将这些打印语句移动到其中__main__
,您就不会再看到它们了。
推荐阅读
- html - 单选按钮。复选框在物化 css 中的表格内不可见
- regex - 如何使用 htaccess 为特定域重定向目录?
- c# - 双显示模式下没有分辨率变化
- javascript - 提交redux表单时不可见的reacaptcha位置问题
- node.js - code=H10 desc="App crashed" method=GET path="/",试图在heroku上上传我的项目
- zurb-foundation - Zurb Foundation 的手风琴不兼容 WAI
- regex - 带有双标签的正则表达式到马赫合并代码
- angular - 除了 spring boot 的功能之外,Jhipster 如何支持 thymeleaf?
- javascript - 从 1 开始每个 jquery
- python - 将函数的结果附加到列表中