Python 如何将多处理字典变量重置回默认状态
使用Python 如何将多处理字典变量重置回默认状态,python,multiprocessing,Python,Multiprocessing,使用multiprocessing模块,声明一种“特殊”类型的字典变量poolDict。它用于写入启动的每个多处理进程。从相同的poolDict字典中,主功能可以读取数据。 问题是:在所有过程完成后,如何将poolDict重置为空字典,使其处于默认状态?变量保持“记忆”进程写入的所有数据 后来编辑了代码,以说明poolDict用法更清晰(以及显示如何使用while循环以“实时”方式从poolDict读取进度值)。 导入时间 将多处理作为mp导入 poolDict=mp.Manager().dic
multiprocessing
模块,声明一种“特殊”类型的字典变量poolDict
。它用于写入启动的每个多处理进程。从相同的poolDict
字典中,主功能可以读取数据。
问题是:在所有过程完成后,如何将poolDict
重置为空字典,使其处于默认状态?变量保持“记忆”进程写入的所有数据
后来编辑了代码,以说明poolDict
用法更清晰(以及显示如何使用while
循环以“实时”方式从poolDict
读取进度值)。
导入时间
将多处理作为mp导入
poolDict=mp.Manager().dict()
def myFunct(参数):
打印“myFunct():”,参数
对于范围(110)内的i:
对于范围内的n(500000):
通过
poolDict[arg]=i
打印'myFunct():已完成',参数,poolDict
来自多处理导入池
池=池(进程=2)
myArgsList=['arg1','arg2','arg3']
pool.map\u异步(myFunct、myArgsList)
def打印消息(参数):
打印“\t消息为:”,参数
状态=真
而国家:
如果不是poolDict:
睡眠时间(0.2)
持续
打印消息(poolDict)
检查=正确
如果len(poolDict.keys())您应该能够使用poolDict.clear()
清空dict。在Linux上从命令提示符运行此代码时,看到打印显示没有任何问题。您是如何执行脚本的?谢谢,我刚刚对代码进行了一些编辑,以使poolDict变量对本例更有意义。谢谢你的建议!编辑后,所有的打印都会显示出来,我不会做任何更改。您是如何执行脚本的?在我跨越多进程之后,poolDict
用于根据进程运行的进度更新“main”函数。“main”函数使用此进度信息实时提供基于GUI的PyQt QProgressBars。@Sputnix我的意思是,您实际上是如何运行此测试程序的?命令提示符,空闲,还有别的吗?您提到运行脚本时,myFunct
中没有一个不打印任何内容,但它的打印对我在bash提示符下运行它来说很好。我刚刚开始了一篇关于打印问题的新文章:
import time
import multiprocessing as mp
poolDict=mp.Manager().dict()
def myFunct(arg):
print 'myFunct():', arg
for i in range(110):
for n in range(500000):
pass
poolDict[arg]=i
print 'myFunct(): completed', arg, poolDict
from multiprocessing import Pool
pool = Pool(processes=2)
myArgsList=['arg1','arg2','arg3']
pool.map_async( myFunct, myArgsList)
def printMessage(arg):
print '\t The message is:', arg
state=True
while state:
if not poolDict:
time.sleep(0.2)
continue
printMessage(poolDict)
check=True
if len(poolDict.keys())<len(myArgsList):
check=False
for value in poolDict.values():
if value<105:
check=False
time.sleep(0.2)
print '\n\t\t\t ...SETTING check to', check, 'since one of the values is <105', value
break
if check:
state=False
print '\n\t\t\t\t\t YAHOO!!!!', check, 'len(poolDict.keys())<len(myArgsList):',len(poolDict.keys()), 'vs', len(myArgsList), 'and not a single value is less than 150:', poolDict.values()
break