8

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.

Mike Simpson
  • 208
  • 1
  • 6

3 Answers3

3
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.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • The same issue of threading.activeCount 'starting' above 1 could occur when running under any other managed environment or when a module is not the first to import threading. – Terry Jan Reedy Mar 29 '15 at 23:47
0

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.

Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
  • AFAIK, there are no issues on the python tracker, bugs.python.org, about IDLE and threading. If anyone knows of a real, IDLE-specific issue, please report it there. – Terry Jan Reedy Dec 16 '18 at 20:07
0

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.

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
ebt
  • 1,358
  • 1
  • 13
  • 18
  • 2
    IDLE is written in Tkinter, not WxPython. – Noctis Skytower Aug 23 '12 at 20:23
  • Idle is written in Python and Python code does not directly use the GIL. AFAIK, Idle's use of the threading module is limited to the Threading and Condition classes and the current_thread function. – Terry Jan Reedy Mar 29 '15 at 22:41
  • Meanwhile this may be good advice, this does not appear to be the problem here. The code can be fixed by @TerryJanReedy answer. – The Matt Dec 16 '18 at 17:59