Lightweight Scheduled Task Scheduler library in Python: schedule

Source: Internet
Author: User

When it comes to scheduling scheduled tasks, I believe a lot of people will thinkCeleryCelery, or just write a script and plug it into the crontab. However, a small timing script, to use celery words too "heavy". So, I found a lightweight scheduled Task Scheduler library: schedule. The installation of the library or the simplest pip install schedule is easy to understand. We look at the simplest chestnuts:
ImportScheduleImport TimedefJob ():Print("I ' m working ...") Schedule.every (10). Minutes.do (Job) Schedule.every (). Hour.do (Job) Schedule.every (). day.at ("10:30am"). Do (Job) schedule.every (5). to (10). Days.do (Job) Schedule.every (). Monday.do (Job) Schedule.every (). wednesday.at ("13:15"). Do (Job) whileTrue:schedule.run_pending () time.sleep (1)

This is an example given above in the PyPI. This chestnut is so simple that I don't need to explain it. And, through this chestnut, we can also know that schedule is actually just a timer. In the while true Dead Loop, schedule.run_pending () is to keep schedule running, to query the above pile of tasks, in the task, you can set a different time to run. It's similar to crontab. However, if multiple tasks are running, they are actually executed from top to bottom in order. If the task above is more complex, it will affect the running time of the task below. For example, we do:
ImportdatetimeImportScheduleImport Timedefjob1 ():Print("I ' m working for Job1") Time.sleep (2)    Print("JOB1:", Datetime.datetime.now ())defjob2 ():Print("I ' m working for JOB2") Time.sleep (2)    Print("JOB2:", Datetime.datetime.now ())defrun (): Schedule.every (10). Seconds.do (JOB1) schedule.every (10). Seconds.do (JOB2) whileTrue:schedule.run_pending () time.sleep (1)

Next you will find that two scheduled tasks are not run for 10 seconds, but 12 seconds. Yes. Due to the execution time of JOB1 and JOB2 itself, the task was delayed. In fact, the solution is also very simple: multi-threaded/multiple processes. Don't be naïve to ask me "multithreading in Python is not useless?" "That's not the same thing. When a thread is opened, the job is run independently, not the CPU time of the main process, and the schedule does not spend the time it takes to execute a task, its overhead is just the time it takes to open a thread, so the next execution becomes 10 seconds instead of 12 seconds later.
ImportdatetimeImportScheduleImportThreadingImport Timedefjob1 ():Print("I ' m working for Job1") Time.sleep (2)    Print("JOB1:", Datetime.datetime.now ())defjob2 ():Print("I ' m working for JOB2") Time.sleep (2)    Print("JOB2:", Datetime.datetime.now ())defJob1_task (): Threading. Thread (Target=job1). Start ()defJob2_task (): Threading. Thread (Target=job2). Start ()defrun (): Schedule.every (10). Seconds.do (Job1_task) schedule.every (10). Seconds.do (Job2_task) whileTrue:schedule.run_pending () time.sleep (1)

It's that simple. The only thing to note is that the job should not be a dead loop type, that is, the thread should have an exit to execute. One is because the thread in case of zombie, it will be a very difficult problem, and the next scheduled task will also open a new thread, the number of executions will become a disaster. If the schedule time interval is set to be shorter than the job execution time, the thread will accumulate as a disaster, so you need to pay attention. Schedule This library is relatively simple to use, not a lot of content. I introduce the approximate usage is basically enough, but also want to know other features, you can refer to the official website: https://schedule.readthedocs.io/en/stable/on the sauce.

Lightweight Scheduled Task Scheduler library in Python: schedule

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.