[英]PyGTK blocking non-GUI threads
我想用 PyGTK 玩線程錯誤。 到目前為止我有這個代碼:
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import threading
from time import sleep
class SomeNonGUIThread(threading.Thread):
def __init__(self, tid):
super(SomeNonGUIThread, self).__init__()
self.tid = tid
def run(self):
while True:
print "Client #%d" % self.tid
sleep(0.5)
class App(threading.Thread):
def __init__(self):
super(App, self).__init__()
self.window = gtk.Window()
self.window.set_size_request(300, 300)
self.window.set_position(gtk.WIN_POS_CENTER)
self.window.connect('destroy', gtk.main_quit)
self.window.show_all()
def run(self):
print "Main start"
gtk.main()
print "Main end"
if __name__ == "__main__":
app = App()
threads = []
for i in range(5):
t = SomeNonGUIThread(i)
threads.append(t)
# Ready, set, go!
for t in threads:
t.start()
# Threads work so well so far
sleep(3)
# And now, they freeze :-(
app.start()
它確實使 NonGUIThreads 同時運行了 3 秒。 之后,顯示 window 並停止其他線程,關閉window后。 線程再次運行。
gtk.main()
怎么可能阻塞其他線程? 線程不依賴於任何鎖,因此它們應該在顯示 window 期間工作。
在執行任何與 Gtk 相關的操作之前,您必須調用gobject.threads_init()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.