When it comes to scheduling scheduled tasks, it is believed that many people think of celery, or write a script that plugs into crontab. However, a small timing script, to use celery words too "heavy". So, I found a lightweight scheduled Task Scheduler library: schedule. Task Scheduler, a library of lightweight scheduled task scheduling: schedule. The installation of the library or the simplest pip install schedule is easy to understand.
1 ImportSchedule2 Import Time3 4 defJob ():5 Print("I ' m working ...")6 7Schedule.every (10). Minutes.do (Job)8 schedule.every (). Hour.do (Job)9Schedule.every (). day.at ("10:30am"). Do (Job)TenSchedule.every (5). to (10). Days.do (Job) One schedule.every (). Monday.do (Job) ASchedule.every (). wednesday.at ("13:15"). Do (Job) - - whileTrue: the 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:
1 Importdatetime2 ImportSchedule3 Import Time4 5 defjob1 ():6 Print("I ' m working for Job1")7Time.sleep (2)8 Print("JOB1:", Datetime.datetime.now ())9 Ten defjob2 (): One Print("I ' m working for JOB2") ATime.sleep (2) - Print("JOB2:", Datetime.datetime.now ()) - the 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.
1 Importdatetime2 ImportSchedule3 ImportThreading4 Import Time5 6 defjob1 ():7 Print("I ' m working for Job1")8Time.sleep (2)9 Print("JOB1:", Datetime.datetime.now ())Ten One defjob2 (): A Print("I ' m working for JOB2") -Time.sleep (2) - Print("JOB2:", Datetime.datetime.now ()) the - defjob1_task (): -Threading. Thread (target=job1). Start () - + defjob2_task (): -Threading. Thread (target=job2). Start () + A defrun (): atSchedule.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/Example:
1 Importdatetime2 ImportSchedule3 Import Time4 5 defjob1 ():6 Print("I ' m working for Job1")7Time.sleep (2)8 Print("JOB1:", Datetime.datetime.now ())9 Ten defjob2 (): One Print("I ' m working for JOB2") ATime.sleep (2) - Print("JOB2:", Datetime.datetime.now ()) - the defrun (): -Schedule.every (). day.at ("22:19"). Do (JOB1) -Schedule.every (). day.at ("22:19"). Do (JOB2) - #Schedule.every (3). Seconds.do (JOB1) + #Schedule.every (3). Seconds.do (JOB2) - + whileTrue: A schedule.run_pending () atTime.sleep (1) - - - if __name__=="__main__": - -Run ()
Multithreading examples
1 Importdatetime2 ImportSchedule3 ImportThreading4 Import Time5 6 defjob1 ():7 Print("I ' m working for Job1")8Time.sleep (2)9 Print("JOB1:", Datetime.datetime.now ())Ten One defjob2 (): A Print("I ' m working for JOB2") -Time.sleep (2) - Print("JOB2:", Datetime.datetime.now ()) the - defjob1_task (): -Threading. Thread (target=job1). Start () - + defjob2_task (): -Threading. Thread (target=job2). Start () + A at defrun (): -Schedule.every (). day.at ("22:23"). Do (Job1_task) -Schedule.every (). day.at ("22:23"). Do (Job2_task) - #Schedule.every (3). Seconds.do (Job1_task) - #Schedule.every (3). Seconds.do (Job2_task) - in whileTrue: - schedule.run_pending () toTime.sleep (1) + - the if __name__=="__main__": * $ Run ()Panax Notoginseng
Lightweight Scheduled Task Scheduler library in Python: schedule