Re: [问题] multiprocessing.pool能不用name==main吗

楼主: surimodo (好吃棉花糖)   2022-12-01 20:06:26
※ 引述《Pettitte1 (低调奢华有内涵)》之铭言:
: 是这样的 目前已经会用 multiprocessing.pool 来取得结果
: 范例 code 如下
: https://paste.ee/p/DY72q
: 我可以在 if __name__ == '__main__': 之后取得我要的 data "d"
: 如果我后续想要对 d 进行其他各种运算
: 是否就只能在 if __name__ == '__main__': 缩排内进行各种动作?
: 能够不用 if __name__ == '__main__':
: 就直接 run m() 这个 function 吗?
可以考虑 concurrent.futures
应该算python新推的并行处理库
用意就是取代multiprocessing
封装成更高阶API使用上更便利
改了一下 不用 if __name__ == '__main__' 也能跑
https://paste.ee/p/5vjuQ
不过这是创 thread 不是 process
如果一定要process
那就没办法了
另外 ThreadPoolExecutor() 里面能塞你要创几个threads
像是 ThreadPoolExecutor(max_workers=50)
就是 python 要求建议 3.7 以上
以上
作者: chang1248w (彩棠)   2022-12-01 20:13:00
这样用thread有绕开硬件锁?
作者: Pettitte1   2022-12-02 21:05:00
感谢回复 是可以用的不过时间反而变长了 应该也是跟Threading一样的问题

Links booklink

Contact Us: admin [ a t ] ucptt.com