似乎IDLE(標準Python Windows安裝的一部分)不會正確執行多線程程序,而不會出現令人討厭的掛起或bugout崩潰。有誰知道一種方法來解決這個問題?Python IDLE與多線程兼容?
當與Python解釋直接執行下面的程序將總是在IDLE但完全正常掛:
import threading, time
printLock = threading.Lock()
def pl(s):
printLock.acquire()
print s
printLock.release()
class myThread(threading.Thread):
def run(self):
i = 0
for i in range(0,30):
pl(i)
time.sleep(0.1)
t = myThread()
t.start()
while threading.activeCount() > 1:
time.sleep(1)
pl(time.time())
print "all done!"
輸出樣本:使用IDLE「運行模塊」時
U:\dev\py\multithreadtest>python mt.py
0
1
2
3
4
5
6
7
8
9
1277935368.84
10
11
12
13
14
15
16
17
18
19
1277935369.84
20
21
22
23
24
25
26
27
28
29
1277935370.84
1277935371.84
all done!
輸出功能總是在我的機器上顯示23或24行的時候,無限期地掛起。
IDLE是用Tkinter編寫的,而不是WxPython。 – 2012-08-23 20:23:59
空閒是用Python編寫的,Python代碼不直接使用GIL。 AFAIK,Idle對線程模塊的使用僅限於線程和條件類以及current_thread函數。 – 2015-03-29 22:41:14