Implementing Timed Tasks with Python
There are times when we need to execute a program every once in a while, or loop back and forth to perform a certain task. For example, the blogger in the previous post about crawlers, in the realization of a target for online crawling, also need to use real-time tasks.
Four ways to implement timed tasks with Python
- while True: + sleep()
- timers
- Scheduling module schedule
- Task framework APScheduler
Tasks to be completed at regular intervals (briefly defined)
import datetime def Task(): now = () ts = ('%Y-%m-%d %H:%M:%S') print(ts)
Timed tasks using while True: + sleep()
The first thing that comes to mind is definitely on the line while:true + sleep combination, right?
def loopMonitor(): while True: Task() # 3s check once (3)
The disadvantage of this approach is that only synchronous tasks can be implemented and asynchronous tasks cannot be performed
Implementing Timed Tasks with Timers
from threading import Timer def timerMonitor(): Task() t = Timer(3, timerMonitor) ()
The problem that occurs is that when run too many times, an error is reported: Pyinstaller maximum recursion depth exceeded Error Resolution
Maximum recursion depth is reached, and then the idea is to modify the maximum recursion depth
(100000000)
But when it runs until it reaches max CPU, python will just destroy the program, cool 0.0
Implementation of timed tasks using the scheduling module schedule
schedule is a third-party lightweight task scheduling module that can be executed by seconds, minutes, hours, dates or custom events.
You can also add multiple tasks if you want to perform more than one task.
The code can be found below
import schedule def scheduleMonitor(): # Empty tasks () # Create a task to be executed at 3-second intervals (3).(Task) # Create a task to be executed at 2-second intervals (2).(Task) while True: schedule.run_pending()
But he still needs to be used in conjunction with while Ture and takes up much more CPU than several others.
Implementing Timed Tasks with the Task Framework APScheduler
APScheduler is a timed task framework for Python , for the execution of cycle or timed tasks , the framework can not only add , delete timed tasks , but also can be stored in the database to achieve task persistence , it is very easy to use .
from import BlockingScheduler def APschedulerMonitor(): # Create scheduler: BlockingScheduler scheduler = BlockingScheduler() scheduler.add_job(Task, 'interval', seconds=3, id='test_job1') # Add tasks at 5S intervals scheduler.add_job(Task, 'interval', seconds=5, id='test_job2') ()
summarize
1: The loop+sleep approach can be used for simple tests.
2: timer can realize asynchronous timed tasks.
3: schedule can be fixed-point timed execution, but still need while Ture with, and occupy a large memory.
4: APScheduler framework is more powerful, you can add fixed-point and timed tasks directly in it, impeccable.
So I don't need to tell you who to use QAQ
Above is the python implementation of the four ways to achieve the details of the timed task , more information about the python implementation of the timed task please pay attention to my other related articles !