简体   繁体   中英

kwargs/args errors. Passing parameters to apscheduler handler function

passing parameters to apscheduler handler function This didn't work for me, I've tried different syntax variations (see below). There is something more fundamental that I might be missing.

@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()

I think that I've solved the problem by rewriting tick() in a manner:

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

and the add_job:

 scheduler.add_job(tick, trigger='date', next_run_time=run_date, args=(run_date, td.text))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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