Scheduled tasks
A common kind of background task is an action that should run by itself in the background at any given time. Typically, those are managed through a cron daemon or similar system tools by configuring the daemon to run a given Python script at the provided time.
When you have a primary application that needs to perform tasks cyclically (such as expiring caches, resetting password links, flushing a queue of emails to send, or similar tasks), it's not really viable to do so through a cron job as you would need to dump the data somewhere accessible to the other process: on disk, on a database, or any similarly shared storage.
Â
Luckily, the Python standard library has an easy way to schedule tasks that are to be executed at any given time and joined with threads. It can be a very simple and effective solution for scheduled background tasks.
How to do it...
The sched
module provides a fully functioning scheduled tasks executor that we can mix with threads to create a background scheduler...