views:

154

answers:

3

I have thise huge code that I'm going to implement . Before I start that, I want to do a raw_input('Enter something: .'). I want it to sleep for 3 secs and if there's no input, then cancel the raw-input prompt and run the rest of the code. Then the code loops and implements the raw_input again.

I want it so that if somedoes put a raw_input in like 'q', then I want the loop to break.

Is there a simple way to do this? I've been stuck on this forever.

Thank You.

A: 

There are many ways to do this on Unix:

http://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python

but you probably don't want that...?

katrielalex
I need to stay away from signals. Signals cant work with my program because I have to so many calls to other python scripts and os.system commands that I can't keep track which program I"m in. I need to be that the program asks a user for an input and after 3 secs, if there's no input proceed and then ask the question again. In the end I want it to run forever if tehre's no input.
ykmizu
+4  A: 

There's an easy solution that doesn't use threads (at least not explicitly): use select to know when there's something to be read from stdin:

import sys
from select import select

timeout = 10
print "Enter something:",
rlist, _, _ = select([sys.stdin], [], [], timeout)
if rlist:
    s = sys.stdin.readline()
    print s
else:
    print "No input. Moving on..."
rbp
Can't use this cause my company computers are using 2.3.3.. Thanks.
ykmizu
The `select` module is in python 2.3.
Aaron Gallagher
ykmizu: as Aaron pointed out, the select module is available on Python 2.3.3: http://docs.python.org/release/2.3.3/lib/module-select.html
rbp
I'm getting this error: Traceback (most recent call last): File "Z:\test.py", line 6, in <module> rlist, _, _ = select([sys.stdin], [], [], timeout) TypeError: argument must be an int, or have a fileno() method.
ykmizu
Well, the documentation explicitly mentions that "Among the acceptable object types in the lists are Python file objects (e.g. sys.stdin". From your traceback, I assume you're running on Windows, perhaps that's related. Is the final program running on Windows as well?
rbp
No, I'm running everything on unix, solaris
ykmizu
nothing is working.
ykmizu
On Solaris? Did you name the file "Z:\test.py"? Can you try to print "sys.stdin" before calling select, and paste the whole output (the print and the traceback) on http://pastebin.org/?
rbp
http://pastebin.org/476808
ykmizu
I just got it to work. Thank you so much for that code. I am forever indebted to you. You have seriously saved my life and my sanity. Thank you.
ykmizu
I'm glad to hear it :) Since you seem to be new on Stack Overflow, let me give you a gentle nudge: if you find that someone has appropriately answered your question (this one, the other one you asked elsewhere, and any one that you might ask in the future), please don't forget to mark that answer as the correct one. Thanks, and good luck with your project!
rbp
nice. btw, i have to `sys.stdout.flush()` after printing the prompt, to see it
mykhal
A: 

If you're working on Windows you can try the following:

import sys, time, msvcrt

def readInput( caption, default, timeout = 5):
    start_time = time.time()
    sys.stdout.write('%s(%s):'%(caption, default));
    input = ''
    while True:
        if msvcrt.kbhit():
            chr = msvcrt.getche()
            if ord(chr) == 13: # enter_key
                break
            elif ord(chr) >= 32: #space_char
                input += chr
        if len(input) == 0 and (time.time() - start_time) > timeout:
            break

    print ''  # needed to move to next line
    if len(input) > 0:
        return input
    else:
        return default

# and some examples of usage
ans = readInput('Please type a name', 'john') 
print 'The name is %s' % ans
ans = readInput('Please enter a number', 10 ) 
print 'The number is %s' % ans 
Paul
This will work fine from the command line, but it will not work when running inside Eclipse, I'm not sure how to get msvcrt reading the keyboard from inside Eclipse.
Paul