Abstracts:
Most applications built today require some manner of scheduling mechanism. Polling APIs or databases, constantly checking system health, archiving logs, etc. are common examples. Kubernetes
cap (a poem)Apache Mesos
etc. using auto-scaling expansion techniques (Auto-scaling
) software needs to check the status of deployed applications, and to do so they use survival probes that run periodically (Liveness Probe
). Scheduling tasks need to be decoupled from the business logic, so we want to use decoupled execution queues, such asRedis
Queue.
Python
There are several ways to schedule a task at regular intervals, which is what we will learn in this article.I will discuss scheduling tasks using the following:
- Simple loop (
Simple Loops
) - Simple loop but using threads (
Simple Loops but Threaded
) - Dispatch Library (
Schedule Library
) Python Crontab
- RQ scheduler as decoupled queue (
RQ Scheduler as decoupled queues
)
1、Simple loops Simple loops
Using simple loops for scheduling tasks is effortless. Using an infinite-runningwhile
Looped periodic function calls can be used to schedule jobs, but this is not the best way to do it, however it is very effective. You can use the built-in time module'sslleep()
to delay the execution. This is not the way most jobs are scheduled, though, because, well, it looks ugly and is less readable than the other methods.
import time def task(): print("Job Completed!") while 1: task() (10)
Things get a little trickier when it comes to those schedules like 9:00 am every morning or 7:45 pm every Wednesday night.
import datetime def task(): print("Job Completed!") while 1: now = () # schedule at every wednesday,7:45 pm if == 3 and ("%H:%m") == "19:45": task() # sleep for 6 days (6 * 24 * 60 * 60)
This was my first thought of a solution, you're welcome! One of the problems with this approach is that the logic here is blocking, i.e. once thepython
This code is found in the project and it gets stuck in a while 1 loop, blocking the execution of other code.
2, simple loops but threaded Simple loops but threaded
Threading is a concept in computer science. Small programs with their own instructions are executed by processes and managed independently, which solves the blocking situation of our first method, let's see how.
import time import threading def task(): print("Job Completed!") def schedule(): while 1: task() (10) # makes our logic non blocking thread = (target=schedule) ()
After a thread is started, its underlying logic cannot be modified by the main thread, so we may need to add resources through which the program can examine specific scenarios and execute logic based on them.
3、Timed Scheduling Library Schedule Library
Earlier, I said that usingwhile
Looping for scheduling looks ugly, and scheduling libraries can solve this problem.
import schedule import time def task(): print("Job Executing!") # for every n minutes (10).(task) # every hour ().(task) # every daya at specific time ().("10:30").do(task) # schedule by name of day ().(task) # name of day with time ().("13:15").do(task) while True: schedule.run_pending() (1)
As you can see, by doing this we can create multiple scheduling plans effortlessly. I especially like the way jobs are created and the chain of methods (Method Chaining
), on the other hand, this clip has awhile
loop, which means the code is blocking, but I'm sure you already know what can help us with that.
4、Python Crontab
Liunx
hit the nail on the headcrontab
Utilities are an easy-to-use and widely accepted scheduling solution.Python
storehousepython-crontab
provides an API to use thePython
hit the nail on the headCLI
Tools. In thecrontab
In this case, a timed scheduler uses theunix-cron
The string format ( * ) describes this as a set of five values on a line, which indicates when the job should be executed.python-crontab
will be written in the filecrontab
of the program is converted to a write programming method.
from crontab import CronTab cron = CronTab(user='root') job = (command='my_script.sh') (1) ()
python-crontab
Does not automatically save the plan, it needs to be executedwrite()
method to save the plan. There are many more features and I highly recommend you check out their documentation.
5. RQ Scheduler RQ Scheduler
Some tasks can't be executed immediately, so we need to base theLIFO
maybeFIFO
The isochronous queue system creates task queues and pops up tasks.python-rq
Allow us to do this by using theRedis
as an agent to queue jobs. Entries for new jobs are stored as hash maps with information such ascreated_at
, enqueued_at
, origin
, data
, description
.
The queuing task is controlled by a file namedworker
of program execution.workers
existRedis
There is also an entry in the cache that is responsible for queuing tasks out as well as updating the status of tasks in Redis. Tasks can be queued when needed, but to schedule them, we need therq-scheduler
。
from rq_scheduler import Scheduler queue = Queue('circle', connection=Redis()) scheduler = Scheduler(queue=queue) ( scheduled_time=(), # Time for first execution, in UTC timezone func=func, # Function to be queued args=[arg1, arg2], # Arguments passed into function when executed kwargs={'foo': 'bar'}, # Keyword arguments passed into function when executed interval=60, # Time before the function is called again, in seconds repeat=None, # Repeat this number of times (None means repeat forever) meta={'foo': 'bar'} # Arbitrary pickleable data on the job itself )
RQ worker
(RQ Worker) must be started separately in the terminal or via thepython-rq
Worker startup. Once a task has been triggered, it can be seen in the worker terminal, with separate function callbacks available for both success and failure scenarios.
6. Conclusion
There are a few more libraries for scheduling, but here I've discussed the most common ones. It's worth mentioning thatCelery
,celery
Another advantage of is that users can choose between multiple agents. I appreciate you reading to the end. Check out my other articles as well. Cheers!
To this article on the use of Python timed scheduling tasks on the way to this article, more related to the use of Python timed scheduling tasks on the way to search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!