Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 623
Andrew Avatar asked Sep 24 '26 16:09

Andrew


1 Answers

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))
like image 129
Andrew Avatar answered Sep 27 '26 06:09

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!