0

Is there's a simple code in python to have a input only valid in some specified time, for example, 5 secs. This is to say that it first prompt a input line, then after five seconds, if no input, it jumps over (ignore the input()). Appending my tentative code here (in python 2.7.9, Win7)

import threading,time
global sync
sync=threading.Event()
def timing():
    time.sleep(5)
    if not sync.isSet():
        sync.set()
def Input():
    input('input')
    sync.set()
t=threading.Thread(target=timing)
T=threading.Thread(target=Input)
t.start()
T.start()

But this doesn't work at all. Anything works?

YiFei
  • 1,599
  • 1
  • 13
  • 27

1 Answers1

0

Here is a decorator that you can wrap around a function to add a timeout.

It works on windows as well as linux/macos.

from multiprocessing import Process, Pipe
import sys, os

class TimeoutError(Exception): pass

def spawn(f): # A decorator that calls a function given a pipe and returns the return value through it.                                                                                                                                                             
    def func(pipe, args, kwargs, stdout, stdin):
        sys.stdout = stdout
        sys.stdin = stdin
        pipe.send(f(*args, **kwargs)) #Pass the return arguments through the pipe                                                                                                                                                                                   
        pipe.close() # Close the pipe                                                                                                                                                                                                                               
    return func

def timeout(time):
    def decorator(func):
        def run_timeout(*args, **kwargs):
            pipe_1, pipe_2 = Pipe()
            stdin = os.fdopen(os.dup(sys.stdin.fileno()))
            p = Process(target=spawn(func), args=(pipe_2, args, kwargs, sys.stdout, stdin))
            p.start() # Start the function                                                                                                                                                                                                                          
            p.join(time) # Join it back to the main process, max time seconds                                                                                                                                                                                       
            if p.is_alive(): # If it timed out                                                                                                                                                                                                                      
                p.terminate() # Kill the process                                                                                                                                                                                                                    
                raise TimeoutError("Function %s timed out. (Took %s seconds)"%(repr(func), time)) # raise the error                                                                                                                                                 
            return pipe_1.recv() # return the return value                                                                                                                                                                                                          
        return run_timeout
    return decorator

Use it like this

@timeout(5) # Times out after 5 seconds
def get_input(prompt):
    return raw_input(prompt)

If the function doesn't return, it raises a TimeoutError

EDIT: Fixed so that stdin could be used in the second process.

muddyfish
  • 2,954
  • 27
  • 35
  • As I'm very unfamiliar with multiprocessing, I copied your code, and tested directly. A Pickling Error was raised PicklingError: Can't pickle : it's not found as __main__.func – YiFei Aug 26 '15 at 15:24
  • What's the traceback (and your python version)? (I have seen the pickling error before whilst writing it though) – muddyfish Aug 26 '15 at 15:25
  • I'm running Python 2.7.9 on win7 ultimate x64. Ugh, the comment space is not enough for paste it. But the first line in the trace back is get_input(), and the second is p.start(). And finally ForkingPickler(file, protocol).dump(obj), this called the pickle.py – YiFei Aug 26 '15 at 15:33
  • I think it should work now. – muddyfish Aug 26 '15 at 15:44
  • Oh no this time stdin = os.fdopen(os.dup(sys.stdin.fileno())) UnsupportedOperation: fileno – YiFei Aug 26 '15 at 16:03