Four Ways to Realize Timing Tasks in python

  • 2021-10-16 02:12:51
  • OfStack

Directory uses Python to realize timing task
Four Methods of Realizing Timing Task with Python
Using while True: + sleep () to realize timing task
Using threading. Timer Timer to Realize Timing Task Using Scheduling Module schedule to Realize Timing Task
Using task framework APScheduler to realize timing task
Summarize

Realization of Timing Task with Python

Sometimes we need to execute a program every 1 period of time, or perform a certain task in a reciprocating cycle. For example, the crawler 1 mentioned by the blogger in the previous article also needs real-time tasks to crawl a certain target online.

Four Methods of Realizing Timing Task with Python

while True: + sleep() threading. Timer Timer Scheduling module schedule Task Framework APScheduler

Timing Task to be completed (under simple definition)


import datetime
def Task():
  now = datetime.datetime.now()
  ts = now.strftime('%Y-%m-%d %H:%M:%S')
  print(ts)

Using while True: + sleep () to realize timing task

The first thought must be done. while: true + sleep combination, simple and rude, the implementation is as follows


def loopMonitor():
  while True:
    Task()
    # 3s Check 1 Times 
    time.sleep(3)

The disadvantage of this method is that it can only implement synchronous tasks, but cannot perform asynchronous tasks

Using threading. Timer timer to realize timing task


from threading import Timer
def timerMonitor():
  Task()
  t = Timer(3, timerMonitor)
  t.start()

The problem is that when running too many times, an error will be reported: Pyinstaller maximum recursion depth exceeded Error Resolution
Reach the maximum recursion depth, and then think of modifying the maximum recursion depth


sys.setrecursionlimit(100000000)

However, when the maximum CPU is reached, python will directly destroy the program and cool it by 0.0

Using scheduling module schedule to realize timing task

schedule is a third-party lightweight task scheduling module, which can be executed according to seconds, minutes, hours, dates or custom events
If you want to perform multiple tasks, you can also add multiple task
See the code below


import schedule
def scheduleMonitor():
  #  Empty task 
  schedule.clear()
  #  Create 1 Press 3 Execute tasks at intervals of seconds 
  schedule.every(3).seconds.do(Task)
  #  Create 1 Press 2 Execute tasks at intervals of seconds 
  schedule.every(2).seconds.do(Task)
  while True:
    schedule.run_pending()

However, it still needs to be used with while and Ture, and it takes up much more CPU than others.

Using task framework APScheduler to realize timing task

APScheduler is a timed task framework of Python, which is used to execute periodic or timed tasks. This framework can not only add and delete timed tasks, but also store tasks in the database to realize the persistence of tasks, which is very convenient to use.


from apscheduler.schedulers.blocking import BlockingScheduler
def APschedulerMonitor():
  #  Create scheduler: BlockingScheduler
  scheduler = BlockingScheduler()
  scheduler.add_job(Task, 'interval', seconds=3, id='test_job1')
  #  Add Task , Time interval 5S
  scheduler.add_job(Task, 'interval', seconds=5, id='test_job2')
  scheduler.start()

Summarize

1: Loop + sleep can be used for simple testing.
2: timer can implement asynchronous timing tasks.
3: schedule can be executed at fixed point and timing, but it still needs the cooperation of while and Ture, and it occupies a lot of memory.
4: APScheduler framework is more powerful, which can directly add fixed-point and timing tasks, which is impeccable.
So, I don't need to say who to use QAQ

The above is the python timer task implementation of the 4 ways of details, more about python timer task information please pay attention to other related articles on this site!


Related articles: