程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Usage of event scheduling module sched module in Python

編輯:Python

1. Event scheduling

sched The content of the module is very simple , Only one class is defined . It is used as a general event scheduling module .

class sched.scheduler(timefunc, delayfunc) This class defines a common interface for scheduling events , It needs to pass in two parameters externally ,timefunc Is a function that returns a time type number without parameters ( Commonly used, such as time In the module time),delayfunc It should be a call that requires a parameter 、 And timefunc The output of is compatible 、 And acts as a function of delaying multiple time units ( Commonly used as time Modular sleep).

Here is a list :

import sched, time
s = sched.scheduler(time.time, time.sleep) # Generate scheduler 
def print_time():
print "From print_time", time.time()
def print_some_times():
print time.time()
s.enter(5, 1, print_time, ())
# Add scheduling event 
# The four parameters are :
# Interval Events ( The specific value depends on delayfunc, This is seconds );
# priority ( Two events arrive at the same time );
# Trigger function ;
# Function parameter ;
s.enter(10, 1, print_time, ())
# function 
s.run()
print time.time()
if __name__ == '__main__':
print_some_times()

See the output results , Partition 5 Execute the first event in seconds , Partition 10 Execute the second event in seconds :

1499259731.99
From print_time 1499259736.99
From print_time 1499259741.99
1499259741.99

In a multithreaded scenario , There will be thread safety issues ,run() Function blocks the main thread . Official recommended use threading.Timer Class substitution :

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
import time
from threading import Timer
def print_time():
print "From print_time", time.time()
def print_some_times():
print time.time()
Timer(5, print_time, ()).start()
Timer(10, print_time, ()).start()
time.sleep(11) # Block main thread , Wait for the scheduler to finish executing , Then execute the following contents 
print time.time()
if __name__ == '__main__':
print_some_times()

2.Scheduler Object methods

scheduler Object has the following methods or properties :

  • scheduler.enterabs(time, priority, action, argument)

Add an event ,time The argument should be a relative to the one passed to the constructor timefunc Value types compatible with the return value of the function . Events arriving at the same time will follow priority Sequential execution .

Execution event is actually execution action(argument).argument Must be a containing action Sequence of parameters .

The return value is an event , It can be used to cancel the event later ( Please see the cancel()).

  • scheduler.enter(delay, priority, action, argument)

Schedule an event to delay delay Time units . Apart from time , The other parameters 、 Meaning and return value are the same as enterabs() The value of is the same . Inside enterabs Is used to be enter call .

  • scheduler.cancel(event)

Delete event from queue . If the event is not in the current queue , Then the method will run a ValueError.

  • scheduler.empty()

Determines if the queue is empty .

  • scheduler.run()

Run all scheduled events . This function will wait ( Use the... Passed to the constructor delayfunc() function ), Then execute the event , Until there are no more scheduled events .

whatever action or delayfunc Can throw exceptions . In both cases , The scheduler will maintain a consistent state and propagate exceptions . If an exception is caused by action Caused by the , Will not continue to implement run().

  • scheduler.queue

Read-only property , Return a list of upcoming events ( Sort by arrival Events ), Every event has time、priority、action、argument Composed of namedtuple.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved