Junior — Middle
What libraries or approaches do you use for automatic task scheduling in Python?
sobes.tech AI
Answer from AI
In Python, the following libraries and approaches are often used for automatic task scheduling:
- schedule — a simple and user-friendly library for task scheduling with syntax close to natural language.
- APScheduler — a more powerful library supporting various trigger types (cron, intervals, dates), the ability to store tasks in a database, and multithreading.
- Celery with beat — if distributed task execution and integration with message queues are needed.
- Using system schedulers, such as cron on Linux or Task Scheduler on Windows, to run scripts on a schedule.
Example with the schedule library:
import schedule
import time
def job():
print("Task is running")
schedule.every(10).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
This code runs the job() function every 10 minutes.