繁体   English   中英

如何使用flask-crontab 设置带有html 请求的crontab?

[英]How to set a crontab with html request using flask-crontab?

我想运行一个 html 页面,我使用一个按钮来设置一个特定的时间(见下文),然后通过模块flask-crontab运行 cronjob 。 如何在def get_time()之外使用minute , hour , day , month而不设置全局变量?
在这里使用flask-crontab 的可靠方法是什么?

APP = Flask(__name__)
Bootstrap(APP)
crontab = Crontab(APP)

...

@APP.route('/randompage.html' methods = ['POST', 'GET])
def get_time():
    time_req = request.args.get("html_time")
    format_time = datetime.strptime(time_req, "%Y-%m-%dT%H:%M")

    minute = format_time.minute
    hour = format_time.hour
    day = format_time.day
    month = fomrat_time.month

    return render_template('randompage.html', time_req=time_req)


@crontab.job()
def exe_control():
    do something here

html页面上的按钮:

<form action="/randompage.html" method="GET">
<input type="datetime-local" name="html_time"/>
<input type="submit"/></form>

要在其他函数中使用值minute, hour, day, month ,您必须使用global变量或保存在全局list / dictionary或保存在file /database` 中并在其他函数中读取。

但是,如果您希望这些值用作@crontab.job(minute=..., hour=...)那么它是无用的。 您应该像普通函数一样直接在get_time运行它

crontab.job(minute=minute, ...)(exe_control)


APP = Flask(__name__)
Bootstrap(APP)
crontab = Crontab(APP)

# ...

@APP.route('/randompage.html' methods = ['POST', 'GET'])
def get_time():
    time_req = request.args.get("html_time")
    format_time = datetime.strptime(time_req, "%Y-%m-%dT%H:%M")

    minute = format_time.minute
    hour = format_time.hour
    day = format_time.day
    month = fomrat_time.month

    crontab.job(minute=minute, hour=hour, day=day, month=month)(exe_control)

    return render_template('randompage.html', time_req=time_req)

# - without decorator -
def exe_control():
    do something here

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM