Python:为什么池中的管理器dict()显示变量没有在多处理中定义



我的代码是:

from multiprocessing import Process, Manager,Pool
station=["A","B","C"]   
def test(k):
try:
print(phicc)
except Exception as E:
print(E)
print(station[k])


if __name__ == '__main__':

with Manager() as manager:
phicc=manager.dict()
for i in station:

phicc[i]=manager.list()

pool = manager.Pool(processes = 10)
pool.map_async(test,range(len(station)))
pool.close()
pool.join()

然后输出为

name 'phicc' is not defined
A
name 'phicc' is not defined
B
name 'phicc' is not defined
C

我不知道发生了什么!我需要phicc变量可以在test函数中识别!感谢

您需要为多处理池中的每个进程初始化全局变量phicc,方法是使用multiprocessing.pool.Pool构造函数的初始化器initargs参数:

from multiprocessing import Process, Manager, Pool

def init_pool_processes(d):
global phicc
phicc = d
station = ["A", "B", "C"]
def test(k):
try:
print(phicc)
except Exception as E:
print(E)
print(station[k])

if __name__ == '__main__':
with Manager() as manager:
phicc = manager.dict()
for i in station:
phicc[i] = manager.list()
pool = manager.Pool(processes=10, initializer=init_pool_processes, initargs=(phicc,))
pool.map_async(test, range(len(station)))
pool.close()
pool.join()

打印:

{'A': <ListProxy object, typeid 'list' at 0x1e2df934eb0>, 'B': <ListProxy object, typeid 'list' at 0x1e2df940130>, 'C': <ListProxy object, typeid 'list' at 0x1e2df940310>}
{'A': <ListProxy object, typeid 'list' at 0x1e2df934eb0>, 'B': <ListProxy object, typeid 'list' at 0x1e2df940130>, 'C': <ListProxy object, typeid 'list' at 0x1e2df940310>}
{'A': <ListProxy object, typeid 'list' at 0x1e2df934eb0>, 'B': <ListProxy object, typeid 'list' at 0x1e2df940130>, 'C': <ListProxy object, typeid 'list' at 0x1e2df940310>}

票据

我发现使用manager.Pool调用创建的Pool代理而不是创建multiprocessing.pool.Pool类有点奇怪,因为您发布的代码显然不需要这样做。如果使用";标准";池,如果您在Linux或其他使用OSfork创建新进程的平台下运行,那么严格地说,您不必像上面的例子中那样使用池初始值设定项,因为创建的每个新进程都将继承主进程的全局变量(然而,它们不是"可共享的",也就是说,如果子进程修改全局变量,它将更改为自己的本地副本(。因此,以下代码将在Linux下工作:

from multiprocessing import Process, Manager, Pool

station = ["A", "B", "C"]
def test(k):
try:
print(phicc)
except Exception as E:
print(E)
print(station[k])

if __name__ == '__main__':
with Manager() as manager:
phicc = manager.dict()
for i in station:
phicc[i] = manager.list()
# A multiprocessing.pool.Pool instance:
pool = Pool(processes=10)
pool.map_async(test, range(len(station)))
pool.close()
pool.join()

打印:

{'A': <ListProxy object, typeid 'list' at 0x7f8c08a663d0>, 'B': <ListProxy object, typeid 'list' at 0x7f8c08a664c0>, 'C': <ListProxy object, typeid 'list' at 0x7f8c08a662e0>}
{'A': <ListProxy object, typeid 'list' at 0x7f8c08a663d0>, 'B': <ListProxy object, typeid 'list' at 0x7f8c08a664c0>, 'C': <ListProxy object, typeid 'list' at 0x7f8c08a662e0>}
{'A': <ListProxy object, typeid 'list' at 0x7f8c08a663d0>, 'B': <ListProxy object, typeid 'list' at 0x7f8c08a664c0>, 'C': <ListProxy object, typeid 'list' at 0x7f8c08a662e0>}

这就是为什么在发布带有多处理标签的问题时,还要求您使用您的平台(如windowslinux(标记您的问题,因为可能有不同的解决方案。发布的使用池初始值设定项的代码适用于Windows和Linux,但如果您的平台是Linux,则上述仅限Linux的代码会更高效。

最新更新