Mik*_*son 6 python multithreading python-idle
似乎IDLE(标准Python Windows安装的一部分)将不会正确执行多线程程序而不会出现令人讨厌的挂起或错误崩溃.有谁知道解决这个问题的方法?
以下程序将始终挂在IDLE中,但在使用Python解释器直接执行时正常完成:
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!"
Run Code Online (Sandbox Code Playgroud)
样本输出:
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!
Run Code Online (Sandbox Code Playgroud)
使用IDLE时的输出"运行模块"功能始终在我的机器上显示23或24行读数时无限期挂起.
据我所知,在空闲状态下运行线程代码时这是一个错误。IDLE 大量使用 GIL,因此竞争条件和死锁很常见。不幸的是,我对线程不够熟悉,无法提供关于使线程安全的见解,超出了显而易见的范围。
| 归档时间: |
|
| 查看次数: |
1444 次 |
| 最近记录: |