3

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()
1
  • There is no point in defining datetimes or texti in that loop because they just keep getting overwritten
    – roganjosh
    Nov 29, 2019 at 11:25

1 Answer 1

5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.