Multi-threading with a simple timer?

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Jul 3 02:12:57 EDT 2018


David D wrote:
> Is there a SIMPLE method that I can have a TIMER count down at a user input
> prompt - if the user doesn't enter information within a 15 second period, it
> times out.

import signal, sys

def timeout(*args):
     print("Too late!")
     sys.exit(0)

signal.signal(signal.SIGALRM, timeout)
signal.setitimer(signal.ITIMER_REAL, 15)
data = input("Enter something: ")
print("You entered: ", data)

-- 
Greg



More information about the Python-list mailing list