詳解Python3定時(shí)器任務(wù)代碼
更新時(shí)間:2019年09月23日 10:59:05 作者:撒歡
這篇文章主要介紹了Python3定時(shí)器任務(wù)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
使用threading寫的一個(gè)定時(shí)器任務(wù)demo:
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)
總結(jié)
以上所述是小編給大家介紹的Python3定時(shí)器任務(wù)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
您可能感興趣的文章:
- Python while true實(shí)現(xiàn)爬蟲定時(shí)任務(wù)
- jenkins配置python腳本定時(shí)任務(wù)過程圖解
- python BlockingScheduler定時(shí)任務(wù)及其他方式的實(shí)現(xiàn)
- Python定時(shí)任務(wù)APScheduler的實(shí)例實(shí)例詳解
- Linux部署python爬蟲腳本,并設(shè)置定時(shí)任務(wù)的方法
- Python3實(shí)現(xiàn)定時(shí)任務(wù)的四種方式
- Python使用crontab模塊設(shè)置和清除定時(shí)任務(wù)操作詳解
- Python實(shí)現(xiàn)定時(shí)執(zhí)行任務(wù)的三種方式簡單示例
- Python selenium爬蟲實(shí)現(xiàn)定時(shí)任務(wù)過程解析
相關(guān)文章
python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全
柱狀圖(Bar Plot)是一種常用的數(shù)據(jù)可視化方式,用于顯示各個(gè)類別之間的比較,下面這篇文章主要給大家介紹了關(guān)于python?Matplotlib繪制炫酷柱狀圖的藝術(shù)與技巧大全,需要的朋友可以參考下2024-03-03
詳解Pytorch 使用Pytorch擬合多項(xiàng)式(多項(xiàng)式回歸)
這篇文章主要介紹了詳解Pytorch 使用Pytorch擬合多項(xiàng)式(多項(xiàng)式回歸),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
圖文詳解感知機(jī)算法原理及Python實(shí)現(xiàn)
感知機(jī)是二類分類的線性分類模型,其輸入為實(shí)例的特征向量,輸出為實(shí)例的類別(取+1和-1二值)。本文將為大家詳細(xì)講講感知機(jī)算法的原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08
python PyQt5的窗口界面的各種交互邏輯實(shí)現(xiàn)
PyQt5是一個(gè)Python綁定庫,用于Qt C++ GUI框架,它允許開發(fā)者使用Python語言創(chuàng)建跨平臺(tái)的應(yīng)用程序,并利用豐富的Qt圖形用戶界面功能,本文介紹了python中PyQt5窗口界面的各種交互邏輯實(shí)現(xiàn),需要的朋友可以參考下2024-07-07

