【发布时间】:2014-07-24 07:26:02
【问题描述】:
我有一个通过 Tkinter 回调调用的昂贵函数:
def func: # called whenever there is a mouse press in the screen.
print("Busy? " + str(X.busy)) # X.busy is my own varaible and is initialized False.
X.busy = True
do_calculations() # do_calculations contains several tk.Canvas().update() calls
X.busy = False
当我点击太快时,func() 似乎堆积起来,因为 print 给出“Busy? True”,表明该函数尚未完成,我们正在另一个线程上启动它。
但是,print(threading.current_thread()) 总是给出 <_mainthread started>,对于给定的程序运行,每次打印的 123... 总是相同的。同一个线程怎么可能是多个线程?
【问题讨论】:
-
我认为 X.busy 不会启动新线程。不是真正的 tkinter 专家,但我找不到说它确实旋转线程的参考。
-
X 只是一个(静态)类,它包含我的一个变量。 func 从 tkinter 调用。
-
OK - 为什么你认为你的代码会启动一个新线程?另外,您在哪个平台上运行?至少在 Windows 上,Tkinter 回调是由消息循环驱动的,因此都在同一个线程上运行。
-
因为当我运行它时,打印语句返回 True。如果没有多个线程或一个线程被中止,这怎么可能发生?
标签: multithreading python-3.x tkinter