In thread, I have a loop that reads input from the user console. The main thread is busy with a Tkinter mainloop(). How do I terminate this program?
while True: ln = sys.stdin.readline() try: ln = ln[:-1] # Remove LF from line if len(ln)==0: continue # Ignore blank lines ...and so on
The main thead calls startGUI() which contains a tk.mainloop() call. When I press the X close button on the window (this is Linux), Tkinter closes the window and mainloop() returns. I then try to close stdin hoping that sys.stdin will close and cause sys.stdin.readline() will terminate with a nice EOF allowing my stdinLoop thread terminate.
# Start up the GUI window startGUI() # Doesn't return until GUI window is closed, tk.mainloop is called here # # Wait for stdinLoop thread to finish sys.stdin.close() # Hopefully cause stdinTh to close print("waiting for stdinTh to join") stdinTh.join() print("joined stdinTh")
The sys.stdin.realine() never returns after the sys.stdin.close(). (The stdinTh.join() was there to synchronize the closing.)
I think Python readline() is doing something clever (in something called NetCommand) that doesn't return cleanly when stdin is closed.
Does Python think it is evil to have both a Tkinter GUI and use stdin interactively?
I tried using sys.stdin.read(1), but is seems buffer up a line and returns the whole line -- rather than reading one byte/char as I thought a read(1) would.