簡體   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