python任务调度框架_Python任务调度之sched
這次我們主要講解下Python自帶模塊當中的sched,不但小巧,也很強大,在實際應用中,某些場合還是可以用到的。作為一名Linux的SA,我們已經習慣了用crontab,而sched提供了一種延遲處理機制,也可以理解為任務調度的另一種方式的實現。
scheduler.enter(delay, priority, action, argument)
● delay:延遲時間
●?priority:優先級(數越大,優先越低)
●?action:回調函數
●?argument:回調函數的參數
我們來寫一個非常簡單的例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def func(name):
print 'action: %s' % name , time.time()
print 'START:', time.time()
scheduler.enter(3, 2, func, ('fight',))
print 'Middle'
scheduler.enter(3, 1, func, ('make peace',))
scheduler.run()
print 'END:', time.time()
time.sleep(5)
運行結果如下:
START: 1453357480.74
Middle
action: make peace 1453357483.74
action: fight 1453357483.74
END: 1453357483.74
我們再舉一個簡單的例子說明下sched的其它特性:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def func(name):
print 'BEGIN: %s:' % name, time.time()
time.sleep(2)
print 'FINISH %s:' % name, time.time()
print 'START:', time.time()
scheduler.enter(2, 1, func, ('fight',))
scheduler.enter(3, 1, func, ('make peace',))
scheduler.run()
print 'END:', time.time()
time.sleep(20)
運行結果如下:
START: 1339665268.12BEGIN: fight: 1339665270.12FINISH fight: 1339665272.12BEGIN: make peace: 1339665272.12FINISH make peace: 1339665274.12END: 1339665274.12
我們仔細觀察下兩次任務調度的時間間隔,發現是同時運行的?那又是為什么呢?run()一直被阻塞,直到所有事件被全部執行完. 每個事件在同一線程中運行,所以如果一個事件的執行時間大于其他事件的延遲時間,那么,就會產生重疊。重疊的解決方法是推遲后來事件的執行時間。這樣保證 沒有丟失任何事件,但這些事件的調用時刻會比原先設定的遲。
上面的例子第二個事件在第一個事件運行結束后立即運行,因為第一個事件的執行時間足夠長,已經超過第二個事件的預期開始時刻。(本來應該1339660903秒運行)
我們再介紹另外一個保證action在同一時刻執行的函數:
scheduler.enterabs(time, priority, action, argument)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
now = time.time()
def func(name):
print 'action:', time.time(), name
print 'START:', now
scheduler.enterabs(now + 2, 2, func, ('make peace',))
scheduler.enterabs(now + 2, 1, func, ('fight',))
scheduler.run()
print 'END:', now
time.sleep(20)
運行結果如下:
START: 1339666232.38
action: 1339666234.38?fight
action: 1339666234.38?make peace
END: 1339666232.38
因為優先級的關系,所以先fight,然后再make peace,打架是如此重要....總體來講,如果想單純的替換crontab的話,Scheduler框架更加適合,做延遲任務的調度處理的話sched還是可以考慮的。
如果我們想要取消任務調度,可以使用cancel()函數。在上面的例子中出現了阻塞延遲的現象,如果引用線程機制就會避免這種情況的發生,我們簡單舉個例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sched
import threading
import time
scheduler = sched.scheduler(time.time, time.sleep)
counter = 0
def increment_counter(name):
global counter
print 'action: %s' % name , time.time()
counter += 1
print 'counter: ', counter
print 'START:', time.time()
action1 = scheduler.enter(2, 1, increment_counter, ('action1',))
action2 = scheduler.enter(3, 1, increment_counter, ('action2',))
t = threading.Thread(target=scheduler.run)
t.start()
scheduler.cancel(action1)
t.join()
print 'counter:', counter
print 'END:', time.time()
time.sleep(20)
運行結果如下:
START: 1339666987.27
action:?action2?1339666990.27
counter:??1
counter: 1
END: 1339666990.27
因為run()函數會引起阻塞,所以我們需要采用線程機制的方法在另一個線程中通過對象的引用取消任務調度,這里只調度了action2方法。
python使用sched模塊周期性抓取網頁內容
1.使用sched模塊可以周期性地執行指定函數
2.在周期性執行指定函數中抓取指定網頁,并解析出想要的網頁內容,代碼中是六維論壇的在線人數
論壇在線人數統計
#coding=utf-8
import time,sched,os,urllib2,re,string
#初始化sched模塊的scheduler類
#第一個參數是一個可以返回時間戳的函數,第二個參數可以在定時未到達之前阻塞。
s = sched.scheduler(time.time,time.sleep)
#被周期性調度觸發的函數
def event_func():
req = urllib2.Request('http://bt.neu6.edu.cn/')
response = urllib2.urlopen(req)
rawdata = response.read()
response.close()
usernump = re.compile(r'總計 .*? 人在線')
usernummatch = usernump.findall(rawdata)
if usernummatch:
currentnum=usernummatch[0]
currentnum=currentnum[string.index(currentnum,'>')+1:string.rindex(currentnum,'
print "Current Time:",time.strftime('%Y,%m,%d,%H,%M',time.localtime(time.time())),'User num:',currentnum
# 保存結果,供圖表工具amcharts使用
result=open('liuvUserNUm','a')
result.write('{year: new Date('+time.strftime('%Y,%m,%d,%H,%M',time.localtime(time.time()))+'),value:'+currentnum+'},\n')
result.close()
#enter四個參數分別為:間隔事件、優先級(用于同時間到達的兩個事件同時執行時定序)、被調用觸發的函數,給他的參數(注意:一定要以tuple給如,如果只有一個參數就(xx,))
def perform(inc):
s.enter(inc,0,perform,(inc,))
event_func()
def mymain(inc=900):
s.enter(0,0,perform,(inc,))
s.run()
if __name__ == "__main__":
mymain()
本文出自別人博客,請務必保留此出處http://outofmemory.cn/code-snippet/3199/python-usage-sched-module-period-xing-zhuaqu-wangye-content
總結
以上是生活随笔為你收集整理的python任务调度框架_Python任务调度之sched的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: deepin安装java_Deepin
- 下一篇: 产品经理书单:《大数据时代:生活、工作与