This may not be the correct forum but hopefully someone maybe able to help. I am investigating simple threads to be able to have a thread waiting for keyboard input running along with other threads that are doing some processing.
The 2 threads program below executes as I expect in Thonny i.e. the f2 thread runs around the loop and the f1 thread only loops on receipt of an enter key.
In Idle the entire program halts awaiting the enter key for f1 giving the impression that the input() function does not yield allowing the f2 thread to run.
My questions are:-
1) Is the underlying Python engine the same in both Idle and Thonny?
2) Are some Python functions "blocking" in that they cannot be thread switched within the function?
Code: Select all
import _thread # _ in lib name means old lib import time #global x x = 5 # Define functions for the threads def f1( threadName, delay): #delay not used here global x while 1: while input() == None : #waits for input pass x = x + 1 #update global print( "%s: %d" % ( threadName, x)) #print def f2(threadName, delay): while 1: print ("%s: %d" % (threadName, x)) time.sleep(delay) #pause for delay # Create two threads as follows try: _thread.start_new_thread( f1, ("Thread-1", 1) ) _thread.start_new_thread( f2, ("Thread-2", 1) ) #start thread and pass delay except: print ("Error: unable to start thread") # does this line if the above fails while 1: pass #equiv to a nop