我有一些2线程的Python应用程序。每个线程都在一个单独的gui中运行。图形用户界面需要独立运行而不会阻塞。我想弄清楚如何让thread_1触发thread_2中发生的事件?Python线程问题
以下是我希望函数foo以尽可能快的方式以最简单,最优雅的方式触发功能栏的一些代码,而不消耗不必要的资源。以下是我想到的。
bar_trigger=False #global trigger for function bar.
lock = threading.Lock()
class Thread_2(threading.Thread):
def run(self):
global lock, bar_trigger
while(True):
lock.acquire()
if bar_trigger==True:
Thread_2.bar() #function I want to happen
bar_trigger=False
lock.release()
time.sleep(100) #sleep to preserve resources
#would like to preserve as much resources as possible
# and sleep as little as possible.
def bar(self):
print "Bar!"
class Thread_1(threading.Thread):
def foo(self):
global lock, bar_trigger
lock.acquire()
bar_trigger=True #trigger for bar in thread2
lock.release()
有没有更好的方法来实现这个目标?我不是一个线程专家,所以任何有关如何从thread_1中最好地触发thread_2中的方法的建议,我们都会很感激。
我不知道任何支持多线程GUI编程的GUI框架。你在使用哪个框架? – 2011-03-15 23:00:27