Is Python IDLE multithreaded compatible?

It seems that IDLE (part of the standard Python Windows installation) will not execute multithreaded programs correctly without unpleasant freezes or crashes in Bugout. Does anyone know how to fix this?

The following program will always hang in IDLE, but it will execute normally when executed directly with the Python interpreter:

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!

when using the IDLE function, the “Run Module” always freezes endlessly while a 23 or 24 line message appears on my machine.

+5
source share
3 answers

AFAIK crapshoot IDLE. IDLE GIL , . , , , , .

+2

IDLE , . , , IDLE, , . IronPython Python Tools Visual Studio. VS , .

+1
import threading
print(threading.activeCount())

1 , 2 . ,

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

, .

, -

initial_threads = threading.activeCount()

after importing and changing the loop title to

while threading.activeCount() > initial_threads:

With this change, the code goes through 30 cycles and stops "everything is done!". I have added this to the list of console differences between Python and Idle that need to be documented.

+1
source

All Articles