【发布时间】:2012-06-05 13:07:14
【问题描述】:
我需要我的程序并行执行某个功能。但是根据用户与程序的交互方式,该功能会产生不同的结果。我在一个名为 threadGUI.py 的模块中有一个简单的 GUI,它有两个选项:下载和上传。这些选项创建包含与函数相关的变量的字典。这些字典存储在主字典中,主字典存储在 thread_test.py 模块中。这些在一个接一个地执行时工作正常,但是当我尝试并行执行时出现问题。 threadGUI.py中的线程相关代码:
def OnStartClick(self):
for i in thread_test.dictList.values(): #the main dictionary is stored as a global in thread_test.py
thread = Thread(target = thread_test.begin_tests, args = (i, ))
thread.start()
print "thread finished...exiting"
上述函数调用thread_test.py模块中的begin_test函数。函数如下所示:
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)
这是我的代码的简化示例。我的问题是我的代码只执行两个函数中的一个,而不是两个函数。因此,如果我创建了一个名为下载、上传、下载的字典,那么它将执行三个下载,而不是所需的模式。
【问题讨论】:
标签: python function if-statement module python-multithreading