How to schedule an event in python without multithreading?

Maybe sched?


You could use a combination of signal.alarm and a signal handler for SIGALRM like so to repeat the function every 5 seconds.

import signal

def handler(sig, frame):
   print ("I am done this time")
   signal.alarm(5) #Schedule this to happen again.

signal.signal(signal.SIGALRM, handler)   

signal.alarm(5)

The other option is to use the sched module that comes along with Python but I don't know whether it uses threads or not.


Sched is probably the way to go for this, as @eumiro points out. However, if you don't want to do that, then you could do this:

import time
while 1:
    #call your event
    time.sleep(x) #wait for x many seconds before calling the script again

Tags:

Python