HOWTO: schedule repeating events with Python

less than 1 minute read

import sched  
import datetime, time

class PeriodicScheduler(object):                                                  
    def __init__(self):                                                           
        self.scheduler = sched.scheduler(time.time, time.sleep)                   
    def setup(self, interval, action, actionargs=()):                             
        self.scheduler.enter(interval, 1, self.setup,                             
                        (interval, action, actionargs))                           
    def run(self):                                                               

# This is the event to execute every time  
def periodic_event():  

INTERVAL = 1 # every second  
periodic_scheduler = PeriodicScheduler()  
periodic_scheduler.setup(INTERVAL, periodic_event) # it executes the event just once # it starts the scheduler  

Tags: ,

Categories: ,


Leave a comment