简体   繁体   English

Python IDLE与多线程兼容?

[英]Python IDLE compatible with multithreading?

It seems that IDLE (part of the standard Python Windows install) will not execute multithreaded programs correctly without nasty hangs or bugout crashes. 似乎IDLE(标准Python Windows安装的一部分)将不会正确执行多线程程序而不会出现令人讨厌的挂起或错误崩溃。 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: 以下程序将始终挂在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!"

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. 使用IDLE时的输出“运行模块”功能始终在我的机器上显示23或24行读数时无限期挂起。

import threading
print(threading.activeCount())

prints 1 when run at the command line, 2 when run from Idle. 在命令行运行时打印1,从空闲运行时打印2。 So your loop 所以你的循环

while threading.activeCount() > 1:
  time.sleep(1)
  pl( time.time() )

will terminate in the console but continue forever in Idle. 将在控制台终止,但永远在空闲状态下继续。

To fix the problem in the posted code, add something like 要解决已发布代码中的问题,请添加类似的内容

initial_threads = threading.activeCount()

after the import and change the loop header to 导入后,将循环标题更改为

while threading.activeCount() > initial_threads:

With this change, the code runs through 30 cycles and stops with 'all done!'. 通过此更改,代码将运行30个周期并以“全部完成!”停止。 I have added this to my list of console Python versus Idle differences that need to be documented. 我已将此添加到我需要记录的控制台Python与空闲差异列表中。

IDLE has some known issues when it comes to threading. 在涉及线程时,IDLE有一些已知问题。 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. 我不知道为什么这是一个问题的详细信息,因为我尽我所能远离IDLE,但我知道它是。 I would strongly suggest you just go get IronPython and Python Tools for Visual Studio. 我强烈建议你去Visual Studio获取IronPython和Python工具。 VS's debugging tools are absolutely unmatched, especially given the huge library of add-ons. VS的调试工具绝对无与伦比,特别是考虑到庞大的附加库。

AFAIK it's a crapshoot when running threaded code in IDLE. 在IDLE中运行线程代码时,AFAIK是一个crapshoot。 IDLE uses the GIL liberally so race conditions and deadlocks are common. IDLE大量使用GIL,因此竞争条件和死锁很常见。 Unfortunately, I am not well versed enough in threading to offer insight on making this thread-safe, beyond the obvious. 不幸的是,我并没有足够精通线程来提供有关使这个线程安全的见解,超越显而易见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM