views:

109

answers:

2

in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input() function is automatically skipped?

+3  A: 

Rather than go into a bunch of details explaining this solution, I will merely say that it's what you want. :)

jer
cool, but unfortunately, for some reason, the signal module doesnt have a "SIGALRM" attribute
calccrypto
@calccrypto, maybe you're on Windows? signal.SIGALRM is Unix-only (see my answer).
Alex Martelli
Right, sorry, should have noted it was Unix only.
jer
yeah. im using xp
calccrypto
+2  A: 

The signal.alarm 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 threading.Timer instead, using thread.interrupt_main to send a KeyboardInterrupt to the main thread from the timer thread. I.e.:

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

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 has happened, and in your handler for KeyboardInterrupt access that "somewhere" to discriminate which of the two cases occurred).

Edit: I could have sworn this was working but I must have been wrong -- the code above omits the obviously-needed timer.start(), and even with it I can't make it work any more. select.select 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.

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 msvcrt.kbhit, performing a msvcrt.getche (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 untested code I would suggest:

import msvcrt
import time

def raw_input_with_timeout(prompt, timeout=30.0):
    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() > finishat:
                return None

The OP in a comment says he does not want to return None 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 return None;-).

If you don't want to time out just because the user is typing slowly (as opposed to, not typing at all!-), you could recompute finishat after every successful character input.

Alex Martelli
hmm, I upvoted this, but now that I test it, it doesn't seem to work :s. You still have to press Enter (python 2.6.5 on Ubuntu Linux).
catchmeifyoutry
yeah. im testing your code right now, and i set it to 5 seconds, but like catchmeifyoutry said, you still have to wait until enter is pressed
calccrypto
there is also an interesting note in the python thread documentation: Caveat: Threads interact strangely with interrupts: the KeyboardInterrupt exception will be received by an arbitrary thread. (When the signal module is available, interrupts always go to the main thread.)
catchmeifyoutry
so any other idea on how to do this? i think Alex Martelli slightly misunderstood me: im trying to do something like those windows button warnings. after counting down, the input() automatically stops waiting and runs the rest of the program with a default value. i dont want a None answer after an hour of waiting. i dont want to wait past 30 seconds at all
calccrypto
@calccrypto, if you want a default different from `None`, add it as an argument to the function; I've now recoded it as Windows-only (but can't test it as I have no Windows) and done it so that it will terminate in 30 seconds, even if the user **is** slowly typing (rather than wait for 30 seconds of no typing, which seems a much more sensible interface to me) though I also mention how to easily get to a more sane behavior (you'd just need to reset the deadline after every typed character is successfully read, so only 30 seconds **of inaction** would result in the timeout behavior).
Alex Martelli
i want to just wait for 30 consecutive seconds of inaction. if nothing is typed, the default value is used. if something is typed but enter is not pressed within 30 seconds, the default value is usedand the new code doesnt work on windowsthe default value doesnt really matter. just say the default value is 3.1415, and the input is waiting for that to be changed. and please excuse my silliness. i have never needed time and thread before
calccrypto
So the code I gave (second, Windows-specific one) should work, just changing `return None` to `return 3.1415`, no?
Alex Martelli
oh wait. that was a typo. you wrote kbkit, not kbhit. its working!!! thanks!!!
calccrypto
Ah yes, that's what comes of untested code, let me fix the typo.
Alex Martelli
wait.. where does the prompt string get used?
calccrypto
@calccrypto, clearly by a `print prompt,` that I had omitted -- I just edited the answer so it's there now (I'm not sure if you need a `sys.stdout.flush()` after it, and I don't have a Windows machine around to try;-).
Alex Martelli