Bonsoir,
J’essaie d’apprendre un peu Flask, et je tente quelques trucs, pour voir comment ça marche. J’ai essayé d’utiliser APScheduler, mais j’échoue.
Le code suivant fonctionne
@app.route('/game', methods=['GET', 'POST'])
@login_required
def game():
form = GameForm()
user = current_user
user.value+=1
db.session.commit()
return render_template('game.html', user=user, form=form)
return render_template('game.html', user=user, form=form)
Mais celui-ci ne fonctionne pas, tout le reste étant complètement identique par ailleurs.
@app.route('/game', methods=['GET', 'POST'])
@login_required
def game():
form = GameForm()
user = current_user
check= user.value
if form.validate_on_submit():
sched=BackgroundScheduler()
sched.start
sched.add_job(addstuff(user,form),'interval',seconds=2)
if (user.value>check+5):
sched.shutdown()
return render_template('game.html', user=user, form=form)
def addstuff(user,form):
user.value+=1
db.session.commit()
return render_template('game.html', user=user, form=form)
Pour ce faire, j’avais essayé de suivre cet exemple.
L’erreur, que je ne comprends pas, est
[2019–05–04 23:49:35,429] ERROR in app: Exception on /game [POST] Traceback (most recent call last): File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise raise value File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/flask_login/utils.py", line 261, in decorated_view return func(*args, kwargs) File "/home/*/dev/Python/Flask/ppbg/app/routes.py", line 87, in game sched.add_job(addstuff(user,form),’interval’,seconds=2) File "/home/***/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/apscheduler/schedulers/base.py", line 434, in add_job job = Job(self, job_kwargs) File "/home/*/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/apscheduler/job.py", line 49, in init self._modify(id=id or uuid4().hex, kwargs) File "/home/*/dev/Python/Flask/ppbg/venv/lib/python3.7/site-packages/apscheduler/job.py", line 170, in _modify raise TypeError(’func must be a callable or a textual reference to one’) TypeError: func must be a callable or a textual reference to one 127.0.0.1 - - [04/May/2019 23:49:35] "POST /game HTTP/1.1" 500 -
Je suis sûr qu’il y a plein de choses que je ne fais pas bien, et je les écouterais volontiers, mais mon problème principal, c’est de comprendre pourquoi le code ne fonctionne pas. Passer de BackgroundScheduler
à BlockingScheduler
ne semble pas aider. Sur la documentation (ici), dans la section "configuring the Scheduler", il y a un certain nombre de lignes que je ne comprends pas et que je n’ai pas ajoutées.
Merci d’avance !