Using module ‘subprocess’ with timeout
Using module ‘subprocess’ with timeout
Using module ‘subprocess’ with timeout
Using a select call is shorter, and should be much more portable import sys, select print “You have ten seconds to answer!” i, o, e = select.select( [sys.stdin], [], [], 10 ) if (i): print “You said”, sys.stdin.readline().strip() else: print “You said nothing!”
You may use the signal package if you are running on UNIX: In [1]: import signal # Register an handler for the timeout In [2]: def handler(signum, frame): …: print(“Forever is over!”) …: raise Exception(“end of time”) …: # This function *may* run for an indetermined time… In [3]: def loop_forever(): …: import time …: … Read more