0

Imagine I had this:

#file main.py

while alive:
    msg = input()
    print(msg)

Now in another thread I evaluate either if 'alive' is True or False. The problem is input() will keep the program hanging.

Is there a way to force it to move on without user interacting with the program? Or can I just terminate the whole program from inside the other thread?

walkman
  • 478
  • 1
  • 8
  • 21
  • Taking input in a thread doesn't kill it or "hang" it. It should still be alive & you should be able to access the `alive` variable from another thread – rdas May 15 '20 at 15:51
  • the input() function waits for user input and I want to know if there's a way to force it to move on and stop wanting input from user – walkman May 15 '20 at 15:52
  • The same thread or another thread? – rdas May 15 '20 at 15:53
  • one thread only evaluates the alive value, main one keeps asking user for input. When alive is set to False i need the main thread to stop asking for that input – walkman May 15 '20 at 15:55
  • `input()` is a blocking call by default, you an try to interrupt the process with some signal handling. See this answer for details: https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout – rdas May 15 '20 at 15:57
  • Use `asyncio` module to code and handle everything concurrently, despite of being single threaded application – Shivam Seth May 15 '20 at 16:05
  • `async` or `await`, will help you working on other things while waiting for input, but as soon as you provide input, while loop will continue – Shivam Seth May 15 '20 at 16:07

0 Answers0