[英]How to create a multiprocessing Queue with set_start_method?
I am trying to create multiprocessing.Queue
that is referenced in a Pool
created within main()
.我正在尝试创建在
main()
中创建的Pool
中引用的multiprocessing.Queue
。 However, using this code:但是,使用此代码:
import multiprocessing as mp
def f(_):
print(b)
def main():
print("main")
print(b)
print("running f")
f(0)
with mp.Pool(2) as pool:
pool.map(f, list(range(2)))
if __name__ == '__main__':
mp.set_start_method('spawn')
b=mp.Queue()
main()
I get NameError: name 'b' is not defined
.我得到
NameError: name 'b' is not defined
。 This code runs fine without the mp.set_start_method('spawn')
call.此代码在没有
mp.set_start_method('spawn')
调用的情况下运行良好。 How can I define a global Queue
object that will be visible to f()
and still use the mp.set_start_method('spawn')
?如何定义对
f()
可见的全局Queue
对象并仍然使用mp.set_start_method('spawn')
?
Use the initializer and initargs arguments of multiprocessing.pool.Pool
to initialize global variable b
for each pool process:使用
multiprocessing.pool.Pool
的初始化器和initargs参数为每个池进程初始化全局变量b
:
import multiprocessing as mp
# Initialize each pool process:
def init_pool(q):
global b
b = q
def f(_):
print(b, b.get())
def main():
print("main")
print(b)
print("running f")
f(0)
with mp.Pool(2, initializer=init_pool, initargs=(b,)) as pool:
pool.map(f, list(range(2)))
if __name__ == '__main__':
mp.set_start_method('spawn')
b=mp.Queue()
b.put(1)
b.put(2)
b.put(3)
main()
Prints:印刷:
main
<multiprocessing.queues.Queue object at 0x00000192041E38E0>
running f
<multiprocessing.queues.Queue object at 0x00000192041E38E0> 1
<multiprocessing.queues.Queue object at 0x000001843E12A190> 2
<multiprocessing.queues.Queue object at 0x000001843E12A190> 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.