Python IDLE compatible with multithreading?

南楼画角 提交于 2019-12-01 17:50:20
ebt

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.

import threading
print(threading.activeCount())

prints 1 when run at the command line, 2 when run from Idle. 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!'. I have added this to my list of console Python versus Idle differences that need to be documented.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!