1

I'd like to have something similar to the following pseudo code:

while input is not None and timer < 5:
    input = getChar()
    timer = time.time() - start

if timer >= 5:
    print "took too long"
else:
    print input

Anyway to do this without threading? I would like an input method that returns whatever has been entered since the last time it was called, or None (null) if nothing was entered.

Josh K
  • 26,152
  • 19
  • 79
  • 130
  • If you use a non-blocking IO message, you'll end up in a hot loop—one that uses all available processing power to take input. You're much better off using `threading.Timer` to trigger the fail condition if it takes too long. – Samir Talwar Jun 16 '10 at 01:23
  • You could `time.sleep` for small periods of time (0.1 seconds or so, maybe less if you need low latency) to avoid a hot loop, or at least cool it down. – intuited Jun 16 '10 at 01:27
  • I *want* the hot loop. That's the question. I can slow it down with the `time.sleep` or whatever. – Josh K Jun 16 '10 at 01:30
  • Please edit your question title to reflect the actual content of your question. "Easy Python input question" tells almost nothing about what the question is. The headline should be more descriptive. It will help people to find and answer your question. – A. Levy Jun 16 '10 at 01:34
  • related "How to set time limit on input" http://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-input – jfs Jun 16 '10 at 02:00

1 Answers1

4

On *nix you want select with sys.stdin. On Windows you want msvcrt.kbhit() and msvcrt.getch().

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283