flask之apscheduler定时任务

flask之apscheduler定时任务_第1张图片
#schedule.py

from flask_apscheduler import APScheduler
from ..dqJSON.GenSchema import getREQRES,generateRule,saveRuleMongo

class Config(object):
    JOBS = [
        {
            'id': 'genReqResRules',
            'func': 'app.Util.schedule:genReqResRules', #这如上面完整路径,不然会报not import module
            'args': ('api-qa3.yonghuivip.com',),
            'trigger': 'interval',
            'seconds': 10
        }
    ]

    SCHEDULER_API_ENABLED = True

def genReqResRules(host):
    r,s = getREQRES(host)
    for rr in r:
        print rr
        rule_schema,_id = generateRule(rr, rr['method'])
        saveRuleMongo(rule_schema,_id)
    for ss in s:
        print ss
        rule_schema,_id = generateRule(ss, ss['method'])
        saveRuleMongo(rule_schema,_id)
    print 'genReqResRules ...'

def QDAPScheduler(app): 
    app.config.from_object(Config())   
    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()

#app.py

from app.Util.schedule import QDAPScheduler
app = Flask(__name__, static_folder='../static',template_folder='../static')
app.debug = DEBUG
#app.secret_key = APP_SECRET_KEY
QDAPScheduler(app)

OK,运行成功!

你可能感兴趣的:(python)