Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://docs.python.org/library/signal.html?highlight=signal#signal.alarm" rel="noreferrer">signal.alarm</a> function, on which @jer's recommended solution is based, is unfortunately Unix-only. If you need a cross-platform or Windows-specific solution, you can base it on <a href="http://docs.python.org/library/threading.html?#timer-objects" rel="noreferrer">threading.Timer</a> instead, using <a href="http://docs.python.org/library/thread.html?highlight=interrupt_main#thread.interrupt_main" rel="noreferrer">thread.interrupt_main</a> to send a <code>KeyboardInterrupt</code> to the main thread from the timer thread. I.e.:</p> <pre><code>import thread import threading def raw_input_with_timeout(prompt, timeout=30.0): print prompt, timer = threading.Timer(timeout, thread.interrupt_main) astring = None try: timer.start() astring = raw_input(prompt) except KeyboardInterrupt: pass timer.cancel() return astring </code></pre> <p>this will return None whether the 30 seconds time out or the user explicitly decides to hit control-C to give up on inputting anything, but it seems OK to treat the two cases in the same way (if you need to distinguish, you could use for the timer a function of your own that, before interrupting the main thread, records somewhere the fact that a timeout <em>has</em> happened, and in your handler for <code>KeyboardInterrupt</code> access that "somewhere" to discriminate which of the two cases occurred).</p> <p><strong>Edit</strong>: I could have sworn this was working but I must have been wrong -- the code above omits the obviously-needed <code>timer.start()</code>, <em>and</em> even with it I can't make it work any more. <code>select.select</code> would be the obvious other thing to try but it won't work on a "normal file" (including stdin) in Windows -- in Unix it works on all files, in Windows, only on sockets.</p> <p>So I don't know how to do a cross-platform "raw input with timeout". A windows-specific one can be constructed with a tight loop polling <a href="http://docs.python.org/library/msvcrt.html?highlight=msvcrt#msvcrt.kbhit" rel="noreferrer">msvcrt.kbhit</a>, performing a <code>msvcrt.getche</code> (and checking if it's a return to indicate the output's done, in which case it breaks out of the loop, otherwise accumulates and keeps waiting) and checking the time to time out if needed. I cannot test because I have no Windows machine (they're all Macs and Linux ones), but here the <strong>untested code</strong> I would suggest:</p> <pre><code>import msvcrt import time def raw_input_with_timeout(prompt, timeout=30.0): print prompt, finishat = time.time() + timeout result = [] while True: if msvcrt.kbhit(): result.append(msvcrt.getche()) if result[-1] == '\r': # or \n, whatever Win returns;-) return ''.join(result) time.sleep(0.1) # just to yield to other processes/threads else: if time.time() &gt; finishat: return None </code></pre> <p>The OP in a comment says he does not want to <code>return None</code> upon timeout, but what's the alternative? Raising an exception? Returning a different default value? Whatever alternative he wants he can clearly put it in place of my <code>return None</code>;-).</p> <p>If you don't want to time out just because the user is typing <em>slowly</em> (as opposed to, not typing at all!-), you could recompute finishat after every successful character input.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload