Common Python Scheduling Techniques and Tools
This article reviews multiple ways to implement periodic tasks in Python, covering simple loops with sleep, libraries such as Timeloop, threading.Timer, sched, schedule, the APScheduler framework, as well as distributed solutions like Celery and Apache Airflow, and provides code examples for each method.
In daily work we often need periodic tasks; besides Linux crond we can use Python. Below are common Python scheduling approaches.
while True + sleep
The time.sleep(secs) function pauses the current thread, allowing a simple loop to act as a scheduler.
<code>import datetime
import time
def time_printer():
now = datetime.datetime.now()
ts = now.strftime('%Y-%m-%d %H:%M:%S')
print('do func time :', ts)
def loop_monitor():
while True:
time_printer()
time.sleep(5) # pause 5 seconds
if __name__ == "__main__":
loop_monitor()
</code>Drawbacks: only interval scheduling, cannot set specific times, and sleep blocks execution.
Timeloop library
Timeloop provides a decorator‑based way to run functions at fixed intervals.
<code>import time
from timeloop import Timeloop
from datetime import timedelta
tl = Timeloop()
@tl.job(interval=timedelta(seconds=2))
def sample_job_every_2s():
print("2s job current time : {}".format(time.ctime()))
@tl.job(interval=timedelta(seconds=5))
def sample_job_every_5s():
print("5s job current time : {}".format(time.ctime()))
@tl.job(interval=timedelta(seconds=10))
def sample_job_every_10s():
print("10s job current time : {}".format(time.ctime()))
</code>threading.Timer
The non‑blocking Timer class can start multiple asynchronous timers.
<code># Example placeholder – actual code omitted for brevity
</code>sched module
The built‑in sched.scheduler offers a generic event scheduler that works with any time and delay functions.
<code>import datetime
import time
import sched
def time_printer():
now = datetime.datetime.now()
ts = now.strftime('%Y-%m-%d %H:%M:%S')
print('do func time :', ts)
def loop_monitor():
s = sched.scheduler(time.time, time.sleep)
s.enter(5, 1, time_printer, ())
s.run()
if __name__ == "__main__":
loop_monitor()
</code>schedule library
A lightweight third‑party scheduler with a human‑readable syntax.
<code>import schedule
import time
def job():
print("I'm working...")
schedule.every(10).seconds.do(job)
# ... other interval specifications ...
while True:
schedule.run_pending()
time.sleep(1)
</code>APScheduler
APScheduler (Advanced Python Scheduler) mirrors Quartz features, supporting date, interval, and cron‑style triggers, with pluggable job stores and executors.
<code>from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
def job():
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
sched = BlockingScheduler()
sched.add_job(job, 'interval', seconds=5, id='my_job_id')
sched.start()
</code>Celery
Celery is a distributed task queue that can also handle periodic tasks via celery beat . It requires a broker (e.g., RabbitMQ or Redis) and a result backend.
Typical architecture: Producer → Broker → Workers → Result Backend.
Apache Airflow
Airflow expresses workflows as DAGs (Directed Acyclic Graphs) and supports a rich set of operators (BashOperator, PythonOperator, etc.). It can run in various executors such as LocalExecutor, CeleryExecutor, or KubernetesExecutor.
Key concepts include DAGs, Operators, Tasks, Task Instances, and the Scheduler/Executor/Worker components.
Overall, the article provides a comprehensive reference for choosing the right Python scheduling tool based on simplicity, scalability, and operational requirements.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.