1 Star 0 Fork 0

joozen/LearnPython

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
python_schedule.py 1.94 KB
Copy Edit Raw Blame History
xianhu authored 2019-08-09 15:56 +08:00 . add py_schedule.py
# _*_ coding: utf-8 _*_
"""
调度的使用
"""
import time
import datetime
from threading import Timer
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
def print_hello():
print("TimeNow in func: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
return
if __name__ == "__main__":
# 1. 使用Threading模块中的Timer
# t = Timer(2, print_hello)
#
# print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
# t.start()
# print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
# exit()
# 2. BlockingScheduler:在进程中运行单个任务,调度器是唯一运行的东西, 采用阻塞的方式
scheduler = BlockingScheduler()
# 采用固定时间间隔(interval)的方式,每隔5秒钟执行一次
scheduler.add_job(print_hello, "interval", seconds=5)
# 采用date的方式,在特定时间只执行一次
# scheduler.add_job(print_hello, "date", run_date=datetime.datetime.now() + datetime.timedelta(seconds=5))
# print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
# scheduler.start()
# print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
# exit()
# 3. BackgroundScheduler: 适合于要求任何在程序后台运行的情况,当希望调度器在应用后台执行时使用。采用非阻塞的方式
scheduler = BackgroundScheduler()
# 采用固定时间间隔(interval)的方式,每隔3秒钟执行一次
scheduler.add_job(print_hello, "interval", seconds=5)
# 这是一个独立的线程
# print("TimeNow start: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
# scheduler.start()
# print("TimeNow end: %s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
# while True:
# time.sleep(2)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/joozen/LearnPython.git
git@gitee.com:joozen/LearnPython.git
joozen
LearnPython
LearnPython
master

Search