Python IDLE 兼容多线程吗?
看起来 IDLE(标准 Python Windows 安装的一部分)不会正确执行多线程程序,除非出现严重的挂起或 bugout 崩溃。有谁知道解决这个问题的方法吗?
以下程序将始终挂在 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!"
示例输出:
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!
使用 IDLE 时的输出“运行模块”函数始终在我的计算机上显示行读数 23 或 24 时无限期挂起。
It seems that IDLE (part of the standard Python Windows install) will not execute multithreaded programs correctly without nasty hangs or bugout crashes. Does anyone know of a way to fix this?
The following program will always hang in IDLE but complete normally when executed with the Python interpreter directly:
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!"
sample output:
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!
output when using IDLE "Run Module" function always hangs indefinitely at around the time the line reading 23 or 24 shows up on my machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在命令行运行时打印 1,从空闲运行时打印 2。因此,您的循环
将在控制台中终止,但在空闲状态下永远继续。
要修复发布的代码中的问题,请
在导入后添加类似内容,并将循环标头更改为
通过此更改,代码将运行 30 个周期并以“全部完成!”停止。我已将其添加到需要记录的控制台 Python 与 Idle 差异列表中。
prints 1 when run at the command line, 2 when run from Idle. So your loop
will terminate in the console but continue forever in Idle.
To fix the problem in the posted code, add something like
after the import and change the loop header to
With this change, the code runs through 30 cycles and stops with 'all done!'. I have added this to my list of console Python versus Idle differences that need to be documented.
据我所知,在空闲状态下运行线程代码时这是一个错误。 IDLE 大量使用 GIL,因此竞争条件和死锁很常见。不幸的是,我对线程不够熟悉,无法提供关于使线程安全的见解,超出了显而易见的范围。
AFAIK it's a crapshoot when running threaded code in IDLE. IDLE uses the GIL liberally so race conditions and deadlocks are common. Unfortunately, I am not well versed enough in threading to offer insight on making this thread-safe, beyond the obvious.
IDLE 在线程方面有一些已知问题。我不太清楚为什么这是一个问题,因为我尽最大努力远离 IDLE,但我知道它确实是一个问题。我强烈建议你去获取 IronPython 和 Visual Studio 的 Python 工具。 VS 的调试工具绝对是无与伦比的,尤其是考虑到庞大的附加组件库。
IDLE has some known issues when it comes to threading. I don't know an overwhelming amount about the particulars of why it's an issue, because I try my very hardest to stay away from IDLE, but I know that it is. I would strongly suggest you just go get IronPython and Python Tools for Visual Studio. VS's debugging tools are absolutely unmatched, especially given the huge library of add-ons.