Sched模塊是標準庫,可用於創建機器人和其他監控和自動化應用程序。 sched 模塊實現了一個通用事件調度程序,用於在特定時間運行任務。它提供了類似於Windows或Linux中的任務調度程序的工具,但主要優點是與Python自己的sched模塊平台差異可以忽略。
例子:
# Python program for Creating
# an event scheduler
import sched
import time
# Creating an instance of the
# scheduler class
scheduler = sched.scheduler(time.time,
time.sleep)
調度程序實例可用的基本方法
- 調度程序.enter():事件可以安排在延遲後或在特定時間運行。為了延遲安排它們,
enter()
使用方法。
用法: scheduler.enter(delay, priority, action, argument=(), kwargs={})
參數:
delay:代表延遲時間的數字
priority:優先值
action:調用函數
argument:參數元組例子:
import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # first event with delay of # 1 second e1 = scheduler.enter(1, 1, print_event, ('1 st', )) # second event with delay of # 2 seconds e1 = scheduler.enter(2, 1, print_event, (' 2nd', )) # executing the events scheduler.run()
輸出:
START: 1580389814.152131 EVENT: 1580389815.1548214 1 st EVENT: 1580389816.1533117 2nd
- 調度程序.enterabs()
enterabs()
time 將事件添加到調度程序的內部隊列中,因為run()
調用調度器的方法,調度器隊列中的條目被一一執行。
用法: scheduler.enterabs(time, priority, action, argument=(), kwargs={})
參數:
time:必須執行事件/操作的時間
priority:事件的優先級
action:構成事件的函數
argument:事件函數的位置參數
kwargs:事件函數的關鍵字參數字典例子:
# library imported import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time and # name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # event x with delay of 1 second # enters queue using enterabs method e_x = scheduler.enterabs(time.time()+1, 1, print_event, argument = ("Event X", )); # executing the events scheduler.run()
輸出:
START: 1580389960.5845037 EVENT: 1580389961.5875661 Event X
- 調度程序.cancel()從隊列中刪除該事件。如果該事件不是當前隊列中的事件,則此方法將引發
ValueError
.
用法: scheduler.cancel(event)
參數:
event:應刪除的事件。import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time and # name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # first event with delay # of 1 second e1 = scheduler.enter(1, 1, print_event, ('1 st', )) # second event with delay # of 2 seconds e2 = scheduler.enter(2, 1, print_event, (' 2nd', )) # removing 1st event from # the event queue scheduler.cancel(e1) # executing the events scheduler.run()
輸出:
START: 1580390119.54074 EVENT: 1580390121.5439944 2nd
- 調度程序.empty()如果事件隊列為空,則返回 True。這不需要爭論。
例子:
import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # checking if event queue is # empty or not print(scheduler.empty()) # event entering into queue e1 = scheduler.enter(2, 1, print_event, ('1 st', )) # checking if event queue is # empty or not print(scheduler.empty()) # executing the events scheduler.run()
輸出:
START: 1580390318.1343799 True False EVENT: 1580390320.136075 1 st
- scheduler.queue隻讀屬性,按事件運行順序返回即將發生的事件的列表。每個事件都顯示為具有以下字段的命名元組:時間、優先級、操作、參數、kwargs。
# library imported import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # event entering into queue e1 = scheduler.enter(2, 1, print_event, ('1 st', )) # printing the details of # upcoming events in event queue print(scheduler.queue) # executing the events scheduler.run()
輸出:
START: 1580390446.8743565
[Event(time=1580390448.8743565, priority=1, action=, argument=(‘1 st’, ), kwargs={})]
EVENT: 1580390448.876916 1 st
相關用法
- Python Scipy stats.alpha()用法及代碼示例
- Python Scipy stats.arcsine()用法及代碼示例
- Python Scipy stats.bayes_mvs()用法及代碼示例
- Python Scipy stats.beta()用法及代碼示例
- Python Scipy stats.betaprime()用法及代碼示例
- Python Scipy stats.binned_statistic()用法及代碼示例
- Python Scipy stats.binned_statistic_2d()用法及代碼示例
- Python Scipy stats.binned_statistic_dd()用法及代碼示例
- Python Scipy stats.bradford()用法及代碼示例
- Python Scipy stats.burr()用法及代碼示例
- Python Scipy stats.cauchy()用法及代碼示例
- Python Scipy stats.chi()用法及代碼示例
- Python Scipy stats.cosine()用法及代碼示例
- Python Scipy stats.cumfreq()用法及代碼示例
- Python Scipy stats.describe()用法及代碼示例
- Python Scipy stats.dgamma()用法及代碼示例
- Python Scipy stats.dweibull()用法及代碼示例
- Python Scipy stats.erlang()用法及代碼示例
- Python Scipy stats.exponpow()用法及代碼示例
- Python Scipy stats.exponweib()用法及代碼示例
- Python Scipy stats.f()用法及代碼示例
- Python Scipy stats.fatiguelife()用法及代碼示例
- Python Scipy stats.fisk()用法及代碼示例
- Python Scipy stats.foldcauchy()用法及代碼示例
- Python Scipy stats.foldnorm()用法及代碼示例
注:本文由純淨天空篩選整理自rakshitarora大神的英文原創作品 Sched module in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。