题
我有一些问题,我的线程应用PyGTK的。我给线程一些时间来完成它的任务,如果有一个问题,我只是继续过,但向用户发出警告。然而,一旦我继续此线程停止,直到gtk.main_quit被调用。这是混淆我。
相关的代码:
class MTP_Connection(threading.Thread):
def __init__(self, HOME_DIR, username):
self.filename = HOME_DIR + "mtp-dump_" + username
threading.Thread.__init__(self)
def run(self):
#test run
for i in range(1, 10):
time.sleep(1)
print i
..........................
start_time = time.time()
conn = MTP_Connection(self.HOME_DIR, self.username)
conn.start()
progress_bar = ProgressBar(self.tree.get_widget("progressbar"),
update_speed=100, pulse_mode=True)
while conn.isAlive():
while gtk.events_pending():
gtk.main_iteration()
if time.time() - start_time > 5:
self.write_info("problems closing connection.")
break
#after this the program continues normally, but my conn thread stops
解决方案
首先,不要子类threading.Thread
,使用Thread(target=callable).start()
。
其次,和可能你的表观块的原因是gtk.main_iteration
需要一个参数block
,默认为True
其中,所以你的呼叫gtk.main_iteration
当没有活动来迭代上实际上会阻塞。其可以与解决:
gtk.main_iteration(block=False)
然而,没有真正的解释,为什么你会使用这个砍死了循环,而不是实际的gtk主循环。如果您已经在主循环中运行这一点,那么我建议你在做错误的事情。我可以在你的选项扩大如果你给我们更多的细节和/或完整的例子。
第三,而这只是想出了以后:总是总是总是的 总是的请确认您已呼吁在线程任何pygtk的应用gtk.gdk.threads_init
。运行时螺纹GTK +具有不同的代码路径,并且它需要知道使用这些。
不隶属于 StackOverflow