Python3 four ways to achieve timing tasks

Python implementation point and timed tasks more ways to find below four implementations, each application has its own way to the scene; the following commonly used to quickly introduce Python implementation of scheduled tasks, look at it
recently made a small program development tasks , is responsible for the background part of the development; according to project requirements, the need to achieve three regular tasks:

1> micro-channel regularly updated token, to update a 2 hours;

2> Goods timing of on-line;

3> timing detection background service is alive;

Using Python to achieve these three tasks, there is need to use the knowledge related to the timing point;

Python implementation point and timed tasks more ways to find the next four in the implementation, each application has its own way to the scene; the following commonly used to quickly introduce Python implementation timing tasks:

1> cycle + sleep;

2> Timer class module thread;

3> schedule module;

4> Timing frame: APScheduler

Before starting to set up a task (so do not rely on external environment):

1: a timing point or monitoring CPU and memory usage;

2: Save time, CPU, memory usage to a log file;

First to implement the system monitoring functions:

Preparation: installation psutil: pip install psutil

Function to achieve

#psutil:获取系统信息模块,可以获取CPU,内存,磁盘等的使用情况
import psutil
import time
import datetime
#logfile:监测信息写入文件
def MonitorSystem(logfile = None):
 #获取cpu使用情况
 cpuper = psutil.cpu_percent()
 #获取内存使用情况:系统内存大小,使用内存,有效内存,内存使用率
 mem = psutil.virtual_memory()
 #内存使用率
 memper = mem.percent
 #获取当前时间
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 line = f'{ts} cpu:{cpuper}%, mem:{memper}%'
 print(line)
 if logfile:
  logfile.write(line)

Code running results:

2019-03-21 14:23:41 cpu:0.6%, mem:77.2%

Next we want to achieve regular monitoring, such as monitoring 3s at the system resource usage.

The easiest way to use: sleep

In this way the most simple and straightforward to use while + sleep can be achieved:

def loopMonitor():
 while True:
  MonitorSystem()
  #2s检查一次
  time.sleep(3)
loopMonitor()

Output:

2019-03-21 14:28:42 cpu:1.5%, mem:77.6%
2019-03-21 14:28:45 cpu:1.6%, mem:77.6%
2019-03-21 14:28:48 cpu:1.4%, mem:77.6%
2019-03-21 14:28:51 cpu:1.4%, mem:77.6%
2019-03-21 14:28:54 cpu:1.3%, mem:77.6%

There is a problem in this way: the timing can only handle a single task.

If you are still in the programming world confused and do not know their future plans

It is a senior development engineer python, python script from basic to web development, reptiles, django, data mining and other projects to combat zero-based data are finishing. Given to every little python partner! Share some of the ways to learn and need to pay attention to small details

Again new tasks: monitoring network needs to send and receive bytes per second, to achieve the following codes:

def MonitorNetWork(logfile = None):
 #获取网络收信息
 netinfo = psutil.net_io_counters()
 #获取当前时间
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 line = f'{ts} bytessent={netinfo.bytes_sent}, bytesrecv={netinfo.bytes_recv}'
 print(line)
 if logfile:
  logfile.write(line)
MonitorNetWork()

Code execution results:

2019-03-21 14:47:21 bytessent=169752183, bytesrecv=1107900973

If we are in a while loop while monitoring mission will have to wait two issues, the situation can not monitor the network per second.

Timer implementation

timer timer basic understanding is that we can start scheduled tasks, these tasks are executed asynchronously timer, so wait for the implementation of the order is not a problem.

First look at the basic use of the Timer:

导入:from threading import Timer

Main methods: Here Insert Picture Description
the timer can only be executed once, if you need to repeat, you need to re-add tasks;

Let's look at the basic use:

from threading import Timer
#记录当前时间
print(datetime.datetime.now())
#3S执行一次
sTimer = Timer(3, MonitorSystem)
#1S执行一次
nTimer = Timer(1, MonitorNetWork)
#使用线程方式执行
sTimer.start()
nTimer.start()
#等待结束
sTimer.join()
nTimer.join()
#记录结束时间
print(datetime.datetime.now())

Output:
Here Insert Picture Description
you can see, it takes time to 3S, but we want to do is to monitor the state of the network per second; how to deal with.

Timer can only be executed once, so you need to add tasks to perform again after completion, we modify the code:

from threading import Timer
import psutil
import time
import datetime
def MonitorSystem(logfile = None):
 cpuper = psutil.cpu_percent()
 mem = psutil.virtual_memory()
 memper = mem.percent
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 line = f'{ts} cpu:{cpuper}%, mem:{memper}%'
 print(line)
 if logfile:
  logfile.write(line)
 #启动定时器任务,每三秒执行一次
 Timer(3, MonitorSystem).start()
def MonitorNetWork(logfile = None):
 netinfo = psutil.net_io_counters()
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 line = f'{ts} bytessent={netinfo.bytes_sent}, bytesrecv={netinfo.bytes_recv}'
 print(line)
 if logfile:
  logfile.write(line)
 #启动定时器任务,每秒执行一次
 Timer(1, MonitorNetWork).start()
MonitorSystem()
MonitorNetWork()

The results: Here Insert Picture Description
you can see from the time, these two tasks can wait no problem at the same time.

The essence is to use the Timer thread way to perform the task, after each execution would be destroyed, so do not worry about resources.

Scheduling module: schedule

schedule a third party is lightweight task scheduling module, in accordance with second, minute, hour, date, or a custom event execution time;

Installation:

pip install schedule

Let's look at an example:

import datetime
import schedule
import time
def func():
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 print('do func time :',ts)
def func2():
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 print('do func2 time:',ts)
def tasklist():
 #清空任务
 schedule.clear()
 #创建一个按秒间隔执行任务
 schedule.every(1).seconds.do(func)
 #创建一个按2秒间隔执行任务
 schedule.every(2).seconds.do(func2)
 #执行10S
 for i in range(10):
  schedule.run_pending()
  time.sleep(1)
tasklist()

The results: Here Insert Picture Description
the implementation process analysis:

1. Because performed at jupyter, it is first emptied task schedule;
2. septum between the time schedule add tasks;
3. Here follow func Add second intervals, according to the two-second interval func2 added;
the task 4.schedule added, need to query tasks and tasks;
5. In order to prevent resource-intensive queries per second mission to the point, then the execution order;

The fifth order of execution to understand how we modify the function func, which added time.sleep (2)

And then perform only func work output: Here Insert Picture Description
you can see the time interval 3S, why is not 1S?

Because the execution order, func sleep 2S, cyclic tasks inquiry sleep 1S, we will have this problem.

在我们使用这种方式执行任务需要注意这种阻塞现象。

我们看下schedule模块常用使用方法:

#schedule.every(1)创建Job, seconds.do(func)按秒间隔查询并执行
schedule.every(1).seconds.do(func)
#添加任务按分执行
schedule.every(1).minutes.do(func)
#添加任务按天执行
schedule.every(1).days.do(func)
#添加任务按周执行
schedule.every().weeks.do(func)
#添加任务每周1执行,执行时间为下周一这一时刻时间
schedule.every().monday.do(func)
#每周1,1点15开始执行
schedule.every().monday.at("12:00").do(job)

这种方式局限性:如果工作任务回非常耗时就会影响其他任务执行。我们可以考虑使用并发机制配置这个模块使用。

任务框架APScheduler

APScheduler是Python的一个定时任务框架,用于执行周期或者定时任务,

可以基于日期、时间间隔,及类似于Linux上的定时任务crontab类型的定时任务;

该该框架不仅可以添加、删除定时任务,还可以将任务存储到数据库中,实现任务的持久化,使用起来非常方便。

安装方式:pip install apscheduler

apscheduler组件及简单说明:

1>triggers(触发器):触发器包含调度逻辑,每一个作业有它自己的触发器

2>job stores(作业存储):用来存储被调度的作业,默认的作业存储器是简单地把作业任务保存在内存中,支持存储到MongoDB,Redis数据库中

3> executors(执行器):执行器用来执行定时任务,只是将需要执行的任务放在新的线程或者线程池中运行

4>schedulers(调度器):调度器是将其它部分联系在一起,对使用者提供接口,进行任务添加,设置,删除。

来看一个简单例子:

import time
from apscheduler.schedulers.blocking import BlockingScheduler
def func():
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 print('do func time :',ts)
def func2():
 #耗时2S
 now = datetime.datetime.now()
 ts = now.strftime('%Y-%m-%d %H:%M:%S')
 print('do func2 time:',ts)
 time.sleep(2)
def dojob():
 #创建调度器:BlockingScheduler
 scheduler = BlockingScheduler()
 #添加任务,时间间隔2S
 scheduler.add_job(func, 'interval', seconds=2, id='test_job1')
 #添加任务,时间间隔5S
 scheduler.add_job(func2, 'interval', seconds=3, id='test_job2')
 scheduler.start()
dojob()

输出结果:Here Insert Picture Description
输出结果中可以看到:任务就算是有延时,也不会影响其他任务执行。

APScheduler框架提供丰富接口去实现定时任务,可以去参考官方文档去查看使用方式。

最后选择:

简单总结上面四种定时定点任务实现:

1:循环+sleep方式适合简答测试,

2:timer可以实现定时任务,但是对定点任务来说,需要检查当前时间点;

3:schedule可以定点定时执行,但是需要在循环中检测任务,而且存在阻塞;

4:APScheduler框架更加强大,可以直接在里面添加定点与定时任务;

Taken together, the decision to use APScheduler framework, simple, just create tasks directly, and can be added to the scheduler.
We recommend learning Python buckle qun: 913066266, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click to join our gathering python learner

Published 43 original articles · won praise 22 · views 60000 +

Guess you like

Origin blog.csdn.net/haoxun06/article/details/104544642