詳解Python3定時器任務(wù)代碼
更新時間:2019年09月23日 10:59:05 作者:撒歡
這篇文章主要介紹了Python3定時器任務(wù)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
使用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()
#取時間點
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實現(xiàn)爬蟲定時任務(wù)
- jenkins配置python腳本定時任務(wù)過程圖解
- python BlockingScheduler定時任務(wù)及其他方式的實現(xiàn)
- Python定時任務(wù)APScheduler的實例實例詳解
- Linux部署python爬蟲腳本,并設(shè)置定時任務(wù)的方法
- Python3實現(xiàn)定時任務(wù)的四種方式
- Python使用crontab模塊設(shè)置和清除定時任務(wù)操作詳解
- Python實現(xiàn)定時執(zhí)行任務(wù)的三種方式簡單示例
- Python selenium爬蟲實現(xiàn)定時任務(wù)過程解析
相關(guān)文章
python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全
柱狀圖(Bar Plot)是一種常用的數(shù)據(jù)可視化方式,用于顯示各個類別之間的比較,下面這篇文章主要給大家介紹了關(guān)于python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全,需要的朋友可以參考下2024-03-03
詳解Pytorch 使用Pytorch擬合多項式(多項式回歸)
這篇文章主要介紹了詳解Pytorch 使用Pytorch擬合多項式(多項式回歸),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
python PyQt5的窗口界面的各種交互邏輯實現(xiàn)
PyQt5是一個Python綁定庫,用于Qt C++ GUI框架,它允許開發(fā)者使用Python語言創(chuàng)建跨平臺的應(yīng)用程序,并利用豐富的Qt圖形用戶界面功能,本文介紹了python中PyQt5窗口界面的各種交互邏輯實現(xiàn),需要的朋友可以參考下2024-07-07

