问题不在发布的代码中:
from threading import Thread
dictList = {
'foo': { 'Type': 'HTTP_Downloading' },
'bar': { 'Type': 'FTP_Uploading' },
'baz': { 'Type': 'HTTP_Downloading' }
}
def begin_tests(arg):
print arg
print dictList
dictItem = arg
print dictItem
if dictItem['Type'] == "HTTP_Downloading":
print "DOWNLOAD"
elif dictItem['Type'] == "FTP_Uploading":
print "UPLOAD"
else:
print "Invalid input"
sys.exit(1)
def OnStartClick(self):
for i in dictList.values(): #the main dictionary is stored as a global in thread_test.py
thread = Thread(target = begin_tests, args = (i, ))
thread.start()
print "thread finished...exiting"
OnStartClick(None)
结果是:
{'Type': 'HTTP_Downloading'}
{'baz': {'Type': 'HTTP_Downloading'}, 'foo': {'Type': 'HTTP_Downloading'}, 'bar': {'Type': 'FTP_Uploading'}}
{'Type': 'HTTP_Downloading'}
{DOWNLOAD
{'Type': 'FTP_Uploading'}
'Type': 'HTTP_Downloading'}
{'baz': {'Type': 'HTTP_Downloading'}, 'foo': {'Type': 'HTTP_Downloading'}, 'bar': {'Type': 'FTP_Uploading'}}
{'baz': {'Type': 'HTTP_Downloading'}, 'foo': {'Type': 'HTTP_Downloading'}, 'bar': {'Type': 'FTP_Uploading'}}
thread finished...exiting
{'Type': 'HTTP_Downloading'}
DOWNLOAD
{'Type': 'FTP_Uploading'}
UPLOAD
猜测一下,您正在重用内部字典。
更新:
我认为这种情况最好通过使用工作池和队列策略来解决。就像是:
from Queue import Queue
from threading import Thread
queue = Queue() # Replaces the dictList
threads = []
for n in range(10):
thread = Thread(target = worker, args = (begin_tests, queue, ))
thread.start()
threads.append(thread)
Stop = object()
def worker(work, queue):
while True:
task = queue.get()
if task is Stop:
break
work(task)
像这样使用它:
queue.put({ 'Type': 'HTTP_Downloading' })
queue.put({ 'Type': 'FTP_Uploading' })
queue.put({ 'Type': 'HTTP_Downloading' })
queue.put(Stop)
这本身并不能解决改变字典的问题;这必须在其他地方修复。这种策略有两个好处:它确实保留了任务的顺序,并且不会冒丢失任务的风险:dict 提供有限的并发保证,而 Queue() 保证是线程安全的。