1

I'm looking to write a program that asks for user_input on a cyclical basis, but does not halt further procedures while waiting for the user to give input.


Is there a command that acts as a press of the RETURN key? Does this have to do with writing to stdin? An alternative might be to see if there is no user_input that currently exists.


Imagine operating the code below, where the program is constantly running, even if the user provides no input,

import os
import time
import termios
import sys

x = 0

while x < 60:
    time.sleep(0.5)
    x += 1
    print x

    user_input = raw_input(" ")

    if user_input == "exit":
        os._exit(1)
Colin Hancey
  • 219
  • 2
  • 8
  • 2
    You can do this with a thread. – kindall Oct 18 '17 at 18:10
  • You will need [multithreading](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) for this. Probably not worth the fuss though. – ichigolas Oct 18 '17 at 18:10
  • [Here's](https://stackoverflow.com/a/45164619/4014959) a demo I wrote that processes user input while performing another task in a separate thread. – PM 2Ring Oct 18 '17 at 18:41
  • I'd really like to divert the conversation away from multithreading, as this appears to be the most complicated solution to my dilemma. It appears to me that there ought to be an extremely simple solution here. Perhaps all I should need to do is to submit /n to the terminal? – Colin Hancey Oct 19 '17 at 22:12
  • 1
    You don't need threading. You can use the `select` or `selectors` module to check the standard input every so often, with a timeout. If nothing is available, just keep doing the other work. If something is available, read it and respond accordingly. – bnaecker Oct 21 '17 at 19:18

0 Answers0