![](/img/trans.png)
[英]Flask, push application context for Flask-sqlalchemy to huey worker
[英]Push application context in Flask
我正在為我最后一年的大學項目開發一個在線投票系統。 多線程 class 給用戶 15 秒的投票時間。 它接受時間戳 cookie 並將其與當前時間進行比較。
class MonitorThread(threading.Thread):
def __init__(self, timecookie):
threading.Thread.__init__(self)
self.timecookie = timecookie
def run(self):
try:
while 1:
timenow = datetime.timestamp(datetime.now())
if timenow - int(float(self.timecookie)) < 15:
continue
else:
return redirect(url_for('index'))
sleep(0.1)
except KeyboardInterrupt:
GPIO.cleanup()
視頻流路由運行用戶的網絡攝像頭以捕獲圖像以進行面部驗證,並運行多線程 class 的實例。
@ app.route('/videostream')
def videostream():
video_stream = VideoCamera()
timecookie = getcookie('time')
MonitorThread(timecookie).start()
return Response(gen(video_stream), mimetype='multipart/x-mixed-replace; boundary=frame')
這會導致錯誤:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/home/abhishek/Documents/sem8/project/myfinalyear/app/app.py", line 48, in run
return redirect(url_for('index'))
File "/home/abhishek/Documents/sem8/project/myfinalyear/env/lib/python3.8/site-packages/flask/helpers.py", line 306, in url_for
raise RuntimeError(
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
我想在時間一到就結束投票過程。 請提出想法。
您是否嘗試過用with
語句包裝它?
with app.app_context():
如果它仍然不起作用,您可以嘗試設置SERVER_NAME
配置,如文檔中所述:
If set, url_for can generate external URLs with only an application context instead of a request context
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.