If you're on macOS or Linux, you can use signals for this -- this doesn't require a lot of modification to your program. Here's an updated version:
import time
import signal
TIMEOUT = 10
a = [ ]
def clear(signum, frame):
global a
a = []
signal.alarm(TIMEOUT)
signal.signal(signal.SIGALRM, clear)
signal.alarm(TIMEOUT)
for i in range (1,10,1):
x = raw_input(" Enter the Value : ")
a.append(x)
print "List Value : " , a
time.sleep(1)
a[ : ] = [ ]
print " List Value : " , a
The only difference here is that a signal handler is installed via signal.signal
which is invoked everytime the signal.SIGALRM
signal is triggered. The code then calls signal.alarm
to cause signal.SIGLARM
to be triggered in 10 seconds. The signal handler (clear
) just clears the list and then schedules another signal.SIGARLM
signal.