August 2018
Intermediate to advanced
366 pages
10h 14m
English
The sched module provides a fully functioning scheduled tasks executor that we can mix with threads to create a background scheduler:
import threading import sched import functools class BackgroundScheduler(threading.Thread): def __init__(self, start=True): self._scheduler = sched.scheduler() self._running = True super().__init__(daemon=True) if start: self.start() def run_at(self, time, action, args=None, kwargs=None): self._scheduler.enterabs(time, 0, action, argument=args or tuple(), kwargs=kwargs or {}) def run_after(self, delay, action, args=None, kwargs=None): self._scheduler.enter(delay, 0, action, argument=args or tuple(), kwargs=kwargs or {}) def run_every(self, seconds, action, args=None, kwargs=None): @functools.wraps(action) ...