kwargs / args错误。将参数传递给apscheduler处理程序函数

问题描述 投票:0回答:1

passing parameters to apscheduler handler function这对我不起作用,我尝试了不同的语法变体(请参见下文)。我可能会缺少一些更基本的东西。

@app.route("/tick", methods=['GET', 'POST'])
def tick(datetimes, texti):
    flash('DO! Sir, you have planned on the {} this:'.format(datetimes), 'success')
    flash(texti, 'info')
    return redirect(url_for('td_list'))

def Schedule_reminders():
    with app.test_request_context():
        if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
            # https://stackoverflow.com/questions/9449101/how-to-stop-flask-from-initialising-twice-in-debug-mode
            scheduler = BackgroundScheduler()
            tds = Td.query.all()
            for td in tds:
                run_date = td.date +' '+ td.time +':00'
                # https://stackoverflow.com/questions/12412708/passing-parameters-to-apscheduler-handler-function/39027779#39027779
                datetimes = run_date
                texti = td.text
                #scheduler.add_job(tick, 'date', [datetimes, texti], run_date)
            # ValueError: dictionary update sequence element #0 has length 1; 2 is required

            #scheduler.add_job(lambda: tick(datetimes, texti), 'date', run_date)
            # ValueError: The list of positional arguments is longer than the target callable can handle (allowed: 0, given in args: 19)

            #scheduler.add_job(tick, 'date', run_date, kwargs={'datetimes':run_date, 'texti':td.text})
            # ValueError: The following arguments are supplied in both args and kwargs: datetimes, texti
            #scheduler.add_job(print_date_time, 'date', run_date)

            scheduler.start()
python flask args kwargs apscheduler
1个回答
0
投票

我认为我已经通过重写tick()的方式解决了这个问题:

def tick(*args):
    flash('DO! Sir, you have planned on the {} this:'.format(args[0]), 'success')
    texti = args[1]
    flash(texti, 'info')

和add_job:

 scheduler.add_job(tick, trigger='date', next_run_time=run_date, args=(run_date, td.text))
© www.soinside.com 2019 - 2024. All rights reserved.