Set up timed tasks with python Lightweight timed task scheduling library in python: schedule

A lightweight timing task scheduling library in python: schedule

 
When it comes to timed task scheduling, I believe many people will think of celery, or write a script and stuff it into crontab. However, a small timing script is too "heavy" to use celery. So, I found a lightweight timing task scheduling library: schedule.
 
The installation of the library is still the simplest pip install schedule, and it is easy to understand to use. Let's start with the simplest chestnut:
 
copy code
import schedule
import time
 
def job():
    print("I'm working...")
 
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).days.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
 
while True:
    schedule.run_pending()
    time.sleep(1)
copy code

 

This is the example given above on pypi. This chestnut is so simple that I don't need to explain it. Moreover, through this chestnut, we can also know that the schedule is actually just a timer. In the while True infinite loop, schedule.run_pending() is to keep the schedule running all the time, to query the above pile of tasks, and in the tasks, you can set different times to run. Similar to crontab.
 
However, if multiple tasks are run, they are actually executed sequentially from top to bottom. If the above task is more complex, it will affect the running time of the following tasks. For example we do:
 
copy code
import datetime
import schedule
import time
 
def job1():
    print("I'm working for job1")
    time.sleep(2)
    print("job1:", datetime.datetime.now())
 
def job2():
    print("I'm working for job2")
    time.sleep(2)
    print("job2:", datetime.datetime.now())
 
def run():
    schedule.every(10).seconds.do(job1)
    schedule.every(10).seconds.do(job2)
 
    while True:
        schedule.run_pending()
        time.sleep(1)
copy code

 

Next you will find that the two scheduled tasks do not run every 10 seconds, but 12 seconds. Yes. The task is delayed due to the execution time of job1 and job2 themselves.
 
In fact, the solution is also very simple: use multi-threading/multi-process. Don't ask me naively "Isn't multithreading in python useless?" It's two different things. When a thread is opened, the job is run independently, and it will not occupy the CPU time of the main process. The schedule does not spend the time to execute a task. Its overhead is only the time to open a thread, so the next execution will change. It became 10 seconds later instead of 12 seconds later.
 
copy code
import datetime
import schedule
import threading
import time
 
def job1():
    print("I'm working for job1")
    time.sleep(2)
    print("job1:", datetime.datetime.now())
 
def job2():
    print("I'm working for job2")
    time.sleep(2)
    print("job2:", datetime.datetime.now())
 
def job1_task():
    threading.Thread(target=job1).start()
 
def job2_task():
    threading.Thread(target=job2).start()
 
def run():
    schedule.every(10).seconds.do(job1_task)
    schedule.every(10).seconds.do(job2_task)
 
    while True:
        schedule.run_pending()
        time.sleep(1)
copy code

 

It's that simple.
 
The only thing to note is that the job should not be an infinite loop type, that is to say, the thread should have an exit that is executed. One is that if the thread freezes, it will be a very difficult problem; the other is that the next timed task will open a new thread, which will turn into a disaster if the number of executions is too high. If the time interval of the schedule is set to be shorter than the execution time of the job, the threads will accumulate and cause a disaster, so you still need to pay attention.
 
The schedule library is relatively simple to use, and the content is not much. The general usage I introduced here is basically enough. If you want to know about other features, you can refer to the official website: https://schedule.readthedocs.io/en/stable/
 
 
 
 https://www.cnblogs.com/anpengapple/p/8051923.html
 

 

When it comes to timed task scheduling, I believe many people will think of celery, or write a script and stuff it into crontab. However, a small timing script is too "heavy" to use celery. So, I found a lightweight timing task scheduling library: schedule.
 
The installation of the library is still the simplest pip install schedule, and it is easy to understand to use. Let's start with the simplest chestnut:
 
copy code
import schedule
import time
 
def job():
    print("I'm working...")
 
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).days.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
 
while True:
    schedule.run_pending()
    time.sleep(1)
copy code

 

This is the example given above on pypi. This chestnut is so simple that I don't need to explain it. Moreover, through this chestnut, we can also know that the schedule is actually just a timer. In the while True infinite loop, schedule.run_pending() is to keep the schedule running all the time, to query the above pile of tasks, and in the tasks, you can set different times to run. Similar to crontab.
 
However, if multiple tasks are run, they are actually executed sequentially from top to bottom. If the above task is more complex, it will affect the running time of the following tasks. For example we do:
 
copy code
import datetime
import schedule
import time
 
def job1():
    print("I'm working for job1")
    time.sleep(2)
    print("job1:", datetime.datetime.now())
 
def job2():
    print("I'm working for job2")
    time.sleep(2)
    print("job2:", datetime.datetime.now())
 
def run():
    schedule.every(10).seconds.do(job1)
    schedule.every(10).seconds.do(job2)
 
    while True:
        schedule.run_pending()
        time.sleep(1)
copy code

 

Next you will find that the two scheduled tasks do not run every 10 seconds, but 12 seconds. Yes. The task is delayed due to the execution time of job1 and job2 themselves.
 
In fact, the solution is also very simple: use multi-threading/multi-process. Don't ask me naively "Isn't multithreading in python useless?" It's two different things. When a thread is opened, the job is run independently, and it will not occupy the CPU time of the main process. The schedule does not spend the time to execute a task. Its overhead is only the time to open a thread, so the next execution will change. It became 10 seconds later instead of 12 seconds later.
 
copy code
import datetime
import schedule
import threading
import time
 
def job1():
    print("I'm working for job1")
    time.sleep(2)
    print("job1:", datetime.datetime.now())
 
def job2():
    print("I'm working for job2")
    time.sleep(2)
    print("job2:", datetime.datetime.now())
 
def job1_task():
    threading.Thread(target=job1).start()
 
def job2_task():
    threading.Thread(target=job2).start()
 
def run():
    schedule.every(10).seconds.do(job1_task)
    schedule.every(10).seconds.do(job2_task)
 
    while True:
        schedule.run_pending()
        time.sleep(1)
copy code

 

It's that simple.
 
The only thing to note is that the job should not be an infinite loop type, that is to say, the thread should have an exit that is executed. One is that if the thread freezes, it will be a very difficult problem; the other is that the next timed task will open a new thread, which will turn into a disaster if the number of executions is too high. If the time interval of the schedule is set to be shorter than the execution time of the job, the threads will accumulate and cause a disaster, so you still need to pay attention.
 
The schedule library is relatively simple to use, and the content is not much. The general usage I introduced here is basically enough. If you want to know about other features, you can refer to the official website: https://schedule.readthedocs.io/en/stable/
 
 
 
 https://www.cnblogs.com/anpengapple/p/8051923.html
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606213&siteId=291194637