import json
import pickle
from flask import Flask, request
from flask_apscheduler import APScheduler # 主要插件
import datetime
app = Flask(__name__)
scheduler = APScheduler()
def task1(a, b):
print('mission_1_',a,b)
print(datetime.datetime.now())
def task2(a, b):
print('mission_2_',a, b)
print(datetime.datetime.now())
# 暂停任务
# scheduler.pause_job('third')
# 恢复任务
# time.sleep(10)
# scheduler.resume_job('third')
# 删除任务
# scheduler.remove_job('first')
@app.route('/pause', methods=['GET'])
def pause_job(): # 暂停
job_id = request.args.get('id')
scheduler.pause_job(str(job_id))
return "pause success!"
@app.route('/resume', methods=['GET'])
def resume_job(): # 恢复
job_id = request.args.get('id')
scheduler.resume_job(str(job_id))
return "Success!"
@app.route('/get_jobs', methods=['GET'])
def get_task(): # 获取
# job_id = request.args.get('id')
jobs = scheduler.get_jobs()
print(jobs)
return 'jobs:'+str(pickle.dumps(jobs))
@app.route('/remove_job', methods=['GET'])
def remove_job(): # 移除
job_id = request.args.get('id')
scheduler.remove_job(str(job_id))
return 'remove success'
# /add_job?id=2
@app.route('/add_job', methods=['GET'])
def add_task():
data = request.args.get('id')
if data == '1':
# trigger='cron' 表示是一个定时任务
scheduler.add_job(func=task1, id='1', args=(1, 1), trigger='cron', day_of_week='0-6', hour=18, minute=24,
second=10, replace_existing=True)
else:
# trigger='interval' 表示是一个循环任务,每隔多久执行一次
scheduler.add_job(func=task2, id='2', args=(2, 2), trigger='interval', seconds=10,
replace_existing=True)
return 'sucess'
# mission_2_ 2 2
# 2019-07-17 18:19:04.663956
# mission_1_ 1 1
# 2019-07-17 18:19:10.010007
# mission_2_ 2 2
# 2019-07-17 18:19:14.664401
if __name__ == '__main__':
scheduler.init_app(app=app)
scheduler.start()
app.run(debug=True, port=8080)
# 法2:
# encodig=utf-8
from flask import Flask, request
from flask_apscheduler import APScheduler
class Config(object): # 创建配置,用类
# 任务列表
JOBS = [
# { # 第一个任务
# 'id': 'job1',
# 'func': '__main__:job_1',
# 'args': (1, 2),
# 'trigger': 'cron', # cron表示定时任务
# 'hour': 19,
# 'minute': 27
# },
{
# 第二个任务,每隔5S执行一次
'id': 'job2',
'func': '__main__:method_test', # 方法名
'args': (1, 2), # 入参
'trigger': 'interval', # interval表示循环任务
'seconds': 5,
}
]
def method_test(a, b):
print(a + b)
app = Flask(__name__)
app.config.from_object(Config()) # 为实例化的flask引入配置
@app.route("/hello", methods=["POST", "GET"])
def check():
return "success", 200
if __name__ == '__main__':
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
app.run(debug=False)
# 其他:
#!usr/env/python
# coding:utf-8
import time
import datetime
# from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.schedulers.background import BackgroundScheduler
def aps_test(x):
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), x)
def scheduler_func():
# scheduler = BlockingScheduler() # 阻塞
scheduler = BackgroundScheduler() # 非阻塞 A scheduler that runs in the background using a separate thread
print(scheduler.get_jobs())
# scheduler.shutdown()
# scheduler = BlockingScheduler()
# date表示具体的一次性任务,interval表示循环任务,cron表示定时任务
scheduler.add_job(func=aps_test, args=('定时任务',), trigger='cron', second='*/1', id='first')
scheduler.add_job(func=aps_test, args=('一次性任务',), trigger='date',
next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=12), id='second')
scheduler.add_job(func=aps_test, args=('循环任务',), trigger='interval', seconds=3, id='third')
print(scheduler.get_jobs())
scheduler.start()
while True:
time.sleep(3)
data=input('输入信息:')
print('running', scheduler.get_jobs())
if data==0:
scheduler.shutdown()
break
print('over')
# 暂停任务
# scheduler.pause_job('third')
# 恢复任务
# time.sleep(10)
# scheduler.resume_job('third')
# 删除任务
# scheduler.remove_job('first')
if __name__ == '__main__':
scheduler_func()
print()
# ------------------
import schedule
import time
def job(name):
print("I'm {},working for my love...".format(name))
name='ouy'
# 每隔5分钟执行一次任务
schedule.every(5).minutes.do(job,name+'1')
# 每隔一小时执行一次任务
schedule.every().hour.do(job,name+'2')
# 每天的10:30执行一次任务
schedule.every().day.at("16:43").do(job,name+'3')
# 每5-10分钟执行一次任务
schedule.every(5).to(10).minutes.do(job,name+'4')
# 每周一的这个时候执行一次任务
schedule.every().monday.do(job,name+'5')
# 每周三的13:30执行一次任务
schedule.every().wednesday.at("16:45").do(job,name+'6')
# 每分钟的这个时刻执行一次任务
schedule.every().minute.at(":47").do(job,name+'7')
while True:
# 启动服务,run_pending()运行所有可以运行的任务
schedule.run_pending()
time.sleep(1)