python-2.7 - 多处理池挂在 jupyter 笔记本中
问题描述
我有一个非常简单的脚本,如下所示:
import multiprocessing as multi
def call_other_thing_with_multi():
P = multi.Pool(3)
P.map(other_thing, range(0,5))
P.join()
def other_thing(arg):
print(arg)
return arg**2.
call_other_thing_with_multi()
当我调用它时,我的代码会永久挂起。这是在带有 python 2.7 的 Windows 上。
感谢您的任何指导!
解决方案
根据文档,您需要先close()
致电join()
:
import multiprocessing as multi
def call_other_thing_with_multi():
P = multi.Pool(3)
P.map(other_thing, range(0,5))
P.close() # <-- calling close before P.join()
P.join()
print('END')
def other_thing(arg):
print(arg)
return arg**2.
call_other_thing_with_multi()
印刷:
0
1
2
3
4
END
编辑:更好的是使用上下文管理器,不要忘记调用close()
:
def call_other_thing_with_multi():
with multi.Pool(3) as P:
P.map(other_thing, range(0,5))
print('END')
推荐阅读
- python - 在数据框的一部分和选定行之间按行划分
- ionic-framework - 如何限制用户从 Ionic 的项目列表中仅选择一项
- date - SSRS 2008 - 为什么某些月份没有返回矩阵中的结果,但结果显示在 SQL 查询窗格中?
- python - 错误系统调用产生 C:\Windows\system32\cmd.exe
- ios - 如何在uitextfield编辑的边框线上添加占位符文本
- r - R从互联网加载数据并同时删除列
- java - 为什么要在另一个列表中投射列表
- jquery - 带有经典 ASP 的 FullCalender.io
- maven - Maven:如何管理从一个子模块到另一个子模块的依赖关系
- .net - 如何配置交换槽功能?目前在部署后我仍然看到旧版本的网络应用程序