forked from Suyixiu/LearnPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_schedule.py
53 lines (40 loc) · 1.94 KB
/
python_schedule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# _*_ 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)