詳解Python3定時器任務(wù)代碼
使用threading寫的一個定時器任務(wù)demo:
import time import sys import signal import datetime import threading #定時器 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() #取時間點(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)
總結(jié)
以上所述是小編給大家介紹的Python3定時器任務(wù)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- Python while true實(shí)現(xiàn)爬蟲定時任務(wù)
- jenkins配置python腳本定時任務(wù)過程圖解
- python BlockingScheduler定時任務(wù)及其他方式的實(shí)現(xiàn)
- Python定時任務(wù)APScheduler的實(shí)例實(shí)例詳解
- Linux部署python爬蟲腳本,并設(shè)置定時任務(wù)的方法
- Python3實(shí)現(xiàn)定時任務(wù)的四種方式
- Python使用crontab模塊設(shè)置和清除定時任務(wù)操作詳解
- Python實(shí)現(xiàn)定時執(zhí)行任務(wù)的三種方式簡單示例
- Python selenium爬蟲實(shí)現(xiàn)定時任務(wù)過程解析
相關(guān)文章
Python使用sql語句對mysql數(shù)據(jù)庫多條件模糊查詢的思路詳解
這篇文章主要介紹了Python使用sql語句對mysql數(shù)據(jù)庫多條件模糊查詢的思路詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Python讀取配置文件(config.ini)以及寫入配置文件
這篇文章主要介紹了Python讀取配置文件(config.ini)以及寫入配置文件,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實(shí)現(xiàn)8個概率分布公式的方法詳解
在本文中,我們將介紹一些常見的分布(均勻分布、高斯分布、對數(shù)正態(tài)分布等)并通過Python代碼進(jìn)行可視化以直觀地顯示它們,感興趣的可以學(xué)習(xí)一下2022-05-05python中內(nèi)置函數(shù)ord()返回字符串的ASCII數(shù)值實(shí)例詳解
ord()?函數(shù)是?chr()?函數(shù)(對于?8?位的?ASCII?字符串)的配對函數(shù),它以一個字符串(Unicode?字符)作為參數(shù),返回對應(yīng)的?ASCII?數(shù)值,或者?Unicode?數(shù)值,這篇文章主要介紹了python?中內(nèi)置函數(shù)ord()返回字符串的ASCII數(shù)值,需要的朋友可以參考下2022-07-07Python中使用socks5設(shè)置全局代理的方法示例
這篇文章主要介紹了Python中使用socks5設(shè)置全局代理的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python 含參構(gòu)造函數(shù)實(shí)例詳解
這篇文章主要介紹了Python 含參構(gòu)造函數(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05