python中的push应用程序上下文

but5z9lq  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(424)

我正在为我大学最后一年的项目开发一个在线投票系统。multi-threading类给用户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()

videostream路由运行用户的网络摄像头来捕获用于人脸验证的图像,还运行多线程类的示例。

@ 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.

时间一到我就想结束投票过程。请提出建议。

f0brbegy

f0brbegy1#

你试过用塑料袋包起来吗 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

相关问题