Python如何實(shí)現(xiàn)定時(shí)器功能
Timer: 隔一定時(shí)間調(diào)用一個(gè)函數(shù),如果想實(shí)現(xiàn)每隔一段時(shí)間就調(diào)用一個(gè)函數(shù)的話(huà),就要在Timer調(diào)用的函數(shù)中,再次設(shè)置Timer。Timer是Thread的一個(gè)派生類(lèi)
python中的線(xiàn)程提供了java線(xiàn)程功能的子集。
#!/usr/bin/env python from threading import Timer import time timer_interval=1 def delayrun(): print 'running' t=Timer(timer_interval,delayrun) t.start() while True: time.sleep(0.1) print 'main running'
t是一個(gè)Timer對(duì)象。delay一秒鐘之后執(zhí)行delayrun函數(shù)。
其中time.sleep函數(shù)是用來(lái)讓主線(xiàn)程暫停一點(diǎn)時(shí)間再繼續(xù)執(zhí)行。
實(shí)例擴(kuò)展:
Python3定時(shí)器任務(wù)代碼
import time import sys import signal import datetime import threading #定時(shí)器 def schedule_update(): t = threading.Timer(0, event_func) t.setDaemon(True) t.start() #執(zhí)行函數(shù) def event_func(): now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(now_time) exec_update() #update_openvas_dbs_from_cache() interval_time = delay_time() t = threading.Timer(interval_time, event_func) t.setDaemon(True) t.start() #取時(shí)間點(diǎn) def delay_time(): # now time now_time = datetime.datetime.now() # tomorrow time next_time = now_time + datetime.timedelta(days=+1) next_year = next_time.date().year next_month = next_time.date().month next_day = next_time.date().day # get tomorrow 00:00 next_time = datetime.datetime.strptime(str(next_year)+"-"+str(next_month)+"-"+str(next_day)+" 00:00:00", "%Y-%m-%d %H:%M:%S") # get secondes delay_time = (next_time - now_time).total_seconds() return delay_time def quit_sys(signum, frame): sys.exit() #接收C if __name__ == "__main__": try: signal.signal(signal.SIGINT, quit_sys) signal.signal(signal.SIGTERM, quit_sys) schedule_update() print("schedule_update server starting up...\nHit Ctrl-C to quit.\n") while 1: time.sleep(1) except Exception as e: print(e)
到此這篇關(guān)于Python如何實(shí)現(xiàn)定時(shí)器功能的文章就介紹到這了,更多相關(guān)Python中的簡(jiǎn)單定時(shí)器實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Opencv實(shí)現(xiàn)圖像模板匹配詳解
模板匹配可以看作是對(duì)象檢測(cè)的一種非?;镜男问健J褂媚0迤ヅ?,我們可以使用包含要檢測(cè)對(duì)象的“模板”來(lái)檢測(cè)輸入圖像中的對(duì)象。本文為大家介紹了圖像模板匹配的實(shí)現(xiàn)方法,需要的可以參考一下2022-09-09Python開(kāi)發(fā)常用五種循環(huán)方式的場(chǎng)景性能比較
Python是一門(mén)高級(jí)編程語(yǔ)言,其擁有多種循環(huán)方式,如for循環(huán)、while循環(huán)、do-while循環(huán)等。本文將逐個(gè)分析Python所有的循環(huán)執(zhí)行效率和適用場(chǎng)景,需要的可以參考一下2023-04-04Python學(xué)習(xí)筆記之Python的下載、腳本與交互模式、注釋
這篇文章主要介紹了Python學(xué)習(xí)筆記之Python的下載、腳本與交互模式、注釋?zhuān)疚膹幕A(chǔ)開(kāi)始學(xué)習(xí)Python,需要的朋友可以參考下2023-03-03淺談django url請(qǐng)求與數(shù)據(jù)庫(kù)連接池的共享問(wèn)題
今天小編就為大家分享一篇淺談django url請(qǐng)求與數(shù)據(jù)庫(kù)連接池的共享問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python?Pandas多種添加行列數(shù)據(jù)方法總結(jié)
在進(jìn)行數(shù)據(jù)分析時(shí)經(jīng)常需要按照一定條件創(chuàng)建新的數(shù)據(jù)列,然后進(jìn)行進(jìn)一步分析,下面這篇文章主要給大家介紹了關(guān)于Python?Pandas多種添加行列數(shù)據(jù)方法的相關(guān)資料,需要的朋友可以參考下2022-07-07Pyinstaller打包Pytorch框架所遇到的問(wèn)題
Pytorch在python界用得比較多,打包容易失敗,本文主要介紹了Pyinstaller打包Pytorch框架所遇到的問(wèn)題,文中介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03