[英]Python Thread does not actually start
I wrote a python GUI program to deal with some scientific calculations. 我编写了一个python GUI程序来处理一些科学计算。 One of the functions was extremely time consuming so that I wish to put it into a separate thread so that running it will not block the entire program. 其中一个功能非常耗时,因此我希望将其放在单独的线程中,以便运行它不会阻塞整个程序。
The problem is that this thread does not actually starts (or queue.get never obtains a result). 问题在于该线程实际上并未启动(或queue.get永远不会获得结果)。
However, if I passed the option block = True into the self.PLUQ = self.queue.get(block = False) statement, the function will execute but will also block the entire program. 但是,如果我将选项block = True传递给self.PLUQ = self.queue.get(block = False)语句,该函数将执行,但也会阻塞整个程序。
Class PLUQ_GUI_Dialog():
#somefunctions to initiatialize the class
def guess_button_click_cb(self):
self.selected_peaks = self.session.selected_peaks()
if (not self.is_ready_to_guess()):
return
self.input_frequencies = self.get_input_frequencies(self.selected_peaks)
self.queue = Queue.Queue()
thread = threading.Thread(target = self.guess, args = (self.queue, self.input_frequencies))
thread.setDaemon(True)
thread.start()
self.top.after(100, self.process_queue)
def guess(self, queue, inputs):
PLUQ = pluq.PLUQ_interface()
#pluq is a separate python module
PLUQ.query(inputs)
#query does some calculations and put results in the PLUQ object
queue.put(PLUQ)
def process_queue(self):
try:
self.PLUQ = self.queue.get(block = False)
self.update_pluq_result() # This will display the result
except Queue.Empty:
self.top.after(100, self.process_queue)
#some helper function
.setDaemon()
is part of the Python 2.6 and earlier API. .setDaemon()
是Python 2.6和更早版本的API的一部分。 You should now be setting your daemon threads like so: 您现在应该像这样设置守护程序线程:
self.queue = Queue.Queue()
thread = threading.Thread(target = self.guess, args = (self.queue, self.input_frequencies))
thread.daemon = True # like this
thread.start()
self.top.after(100, self.process_queue)
The problem could be GIL related. 问题可能与GIL有关。 I got around the problem by using multiprocessing.Process instead of threading.Thread. 我通过使用multiprocessing.Process而不是threading.Thread解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.