Python多線程2:sched

來源:互聯網
上載者:User

標籤:python   程式設計語言   

sched模組提供了一個用於事件調度的類。

scheduler類定義
class sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep) 
scheduler類為事件調度定義了一套通用介面。它需要傳入兩個函數:1)timefunc是一個沒有參數的callable,並且返回一個一個數字(表示“時間”,任意單位)。如果time.monotonic不是可用的,則預設為time.time。2)delayfunc是帶有一個參數的callable,與timefunc的輸出相容,用於延遲一段時間。在多線程應用中,為了給其它線程執行的機會,在每個事件執行後,delayfunc也將使用參數0調用。
從3.3版本開始,scheduler是安全執行緒的。
下面是一個例子:
>>> import sched, time>>> s = sched.scheduler(time.time, time.sleep)>>> def print_time(a='default'):        print("From print_time", time.time(), a)   >>> def print_some_times():        print(time.time())        s.enter(10, 1, print_time)        s.enter(5, 2, print_time, argument=('positional',))        s.enter(5, 1, print_time, kwargs={'a': 'keyword'})        s.run()        print(time.time())   >>> print_some_times()930343690.257From print_time 930343695.274 positionalFrom print_time 930343695.275 keywordFrom print_time 930343700.273 default930343700.276
Scheduler對象
Scheduler執行個體有以下方法和屬性:
scheduler.enterabs(time, priority, action, argument=(), kwargs={})
調度一個新事件。time參數應該是一個數字類型,與構造器傳入的timefunc函數的返回值相容。指定到同一個時間的事件調度將按他們的優先順序順序依次執行。
執行時間即為執行action(*argument, **kwargs)。argument是一個序列,按照action的參數順序排列;kwargs是一個map,使用key對應action的參數。
返回值是一個事件,可以被用於事件的取消(看cancel())。
scheduler.enter(delay, priority, action, argument=(), kwargs={})
在延遲delay時間後調度一個事件。除了使用相對時間,其它的參數和返回值和enterabs是相同的。
scheduler.cancel(event)
從隊列中移除事件。如果事件不在當前隊列中,該方法拋出ValueError。
scheduler.empty()
如果隊列是空的,則返回True。
scheduler.run(blocking=True)
運行所有的事件,這個方法將等待(使用傳遞給構造器的delayfunc()函數)下一個事件的時間到達,然後執行它,直到所有的事件都被執行。
如果blocking為false,則不阻塞等待,立即調度溢出時間的那些時間(如果存在),然後返回在調度器中的下次調度的需要等待的時間,例如:
>>> import sched, time>>> s = sched.scheduler(time.time, time.sleep)>>> def print_time(a='default'):        print("From print_time", time.time(), a)>>> def print_some_times():        print(time.time())        s.enter(10, 1, print_time)        s.enter(5, 2, print_time, argument=('positional',))        s.enter(5, 1, print_time, kwargs={'a': 'keyword'})        print("Next : ",s.run(False))        print(time.time())
第一次調用:
>>> print_some_times()1435115632.601069Next :  5.01435115632.656073
Next表示下一個事件將在5秒後執行。第二次超過10秒後調用:
>>> print_some_times()1435115665.549954From print_time 1435115665.596957 keywordFrom print_time 1435115665.607957 positionalFrom print_time 1435115665.618958 defaultNext :  4.9669978618621831435115665.635959
這時事件已經全部達到執行時間點,所以全部立即執行。
action後者delayfunc能拋出一個異常,這時,調度器將保持一致並傳遞該異常。如果異常被action拋出,以後該事件將不會再被執行。
如果一個事件執行的結束時間超過了下一個事件的執行時間,調度器將忽略下一個事件。沒有事件會被丟棄。
scheduler.queue
將要執行的事件列表,列表唯讀,事件按照將要執行的順序排列。每個事件儲存為一個元組,包含:time、priority、action、argument和kwargs。

Python多線程2:sched

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.