from time import sleep
from random import random
from threading import Timer

def timeout():
    print('Alarm!')
t = Timer(10.0, timeout)
t.start()
sleep(5.0)
if random() < 0.5:
    t.cancel()
print('Canceling')
How to set a timer & clear a timer in python?