Python==3.7.x
有兩種方式實現計劃任務:
-
schedule
-
APscheduler
-
打包python程序
-
定時器:
schedule
import schedule
import time,datetime
def task(name):
print("{0}".format(name))
# 每隔2秒執行一次任務
schedule.every(2).seconds.do(task, name)
# 每隔一小時執行一次任務
schedule.every().hour.do(task, name)
# 每天10:30執行一次任務
schedule.every().day.at("10:30").do(task, name)
while True:
schedule.run_pending()
# 因為schedule只是一個定時器,他不會死循環執行任務,所以我們這里需要使用while
time.sleep(1)
-
調度任務模塊:
apscheduler
# aps有兩種方式寫法
## 第一種(第一種寫法)
from apscheduler.schedulers.blocking import BlockingScheduler
def task():
print('task')
aps = BlockingScheduler()
#在6月,7月,8月,11月和12月的第三個星期五的00:00,01:00,02:00和03:00執行job_function
aps.add_job(job_function, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
aps.start()
## 第二種(第二種寫法,在某些情況下可以避免報錯,比如將python程序打包成執行文件后,第一種方法寫的就會報錯('LookupError: No trigger by the name "cron" was found').)
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.interval import IntervalTrigger
def task():
print('task')
# 每秒執行一次
trigger = IntervalTrigger(seconds=1)
aps = BlockingScheduler()
aps.add_job(func=a._start, trigger=trigger)
aps.start()
-
python程序打包命令
推薦一個制作
icon
的軟件點擊此處下載試用版
pip install PyInstaller==3.5
# 建議使用絕對路徑
pyinstaller main_handler.py -F -p
-i
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
