Heroku gunicorn flask 登录无法正常工作

bqf10yzr  于 2023-01-09  发布在  其他
关注(0)|答案(3)|浏览(125)

我有一个flask应用程序,使用flask-Login进行身份验证。使用flask的内置网络服务器和gunicorn在本地运行时,一切都很好。但当它在heroku上运行时,它会出错,有时它会让我登录,有时不会。当我在导航后几秒钟内成功登录时,我的会话会被破坏,并让我自动注销。这应该在用户注销时发生。
我的视图中的以下代码片段可能与此相关:

@app.before_request
def before_request():
    g.user = current_user

# I have index (/) and other views (/new) decorated with @login_required

我可能也有类似的问题with this。它还没有任何答案,从我从评论中读到的,作者只是用python app.py运行他的应用程序。这是通过使用flask的内置web服务器。然而,我似乎不能重复他的工作方法,因为运行app.run(host='0.0.0.0')在端口5000运行应用程序,我不能't似乎因为权限而设置port=80
我没有看到任何有用的日志,除了它不认证,即使我应该。
当我通过身份验证并尝试交替导航到/new/直到它将我注销时的部分日志:

2016-09-25T06:57:53.052378+00:00 app[web.1]: authenticated - IP:10.179.239.229
2016-09-25T06:57:53.455145+00:00 heroku[router]: at=info method=GET path="/" host=testdep0.herokuapp.com request_id=c7c8f4c9-b003-446e-92d8-af0a81985e72 fwd="124.100.201.61" dyno=web.1 connect=0ms service=116ms status=200 bytes=6526
2016-09-25T06:58:11.415837+00:00 heroku[router]: at=info method=GET path="/new" host=testdep0.herokuapp.com request_id=ae5e4e29-0345-4a09-90c4-36fb64785079 fwd="124.100.201.61" dyno=web.1 connect=0ms service=7ms status=200 bytes=2552
2016-09-25T06:58:13.543098+00:00 heroku[router]: at=info method=GET path="/" host=testdep0.herokuapp.com request_id=47696ab9-57b9-4f20-810a-66033e3e9e50 fwd="124.100.201.61" dyno=web.1 connect=0ms service=8ms status=200 bytes=5982
2016-09-25T06:58:18.037766+00:00 heroku[router]: at=info method=GET path="/new" host=testdep0.herokuapp.com request_id=98912601-6342-4d71-a106-26056e4bbb21 fwd="124.100.201.61" dyno=web.1 connect=0ms service=3ms status=200 bytes=2552
2016-09-25T06:58:19.619369+00:00 heroku[router]: at=info method=GET path="/" host=testdep0.herokuapp.com request_id=2b04d31f-93a2-4653-83a4-f95ca9b97149 fwd="124.100.201.61" dyno=web.1 connect=0ms service=3ms status=302 bytes=640
2016-09-25T06:58:19.953910+00:00 heroku[router]: at=info method=GET path="/login?next=%2F" host=testdep0.herokuapp.com request_id=e80d15cd-e9ad-45ff-ae54-e156412fe4ff fwd="124.100.201.61" dyno=web.1 connect=0ms service=3ms status=200 bytes=2793

程序文件:
web: gunicorn app:app

rqcrx0a6

rqcrx0a61#

这个问题通过在gunicorn中添加--preload选项得到了解决,我不太清楚这是如何解决问题的,如果有人能解释一下,我将不胜感激。
更新的程序文件:
web: gunicorn app:app --preload

prdp8dxp

prdp8dxp2#

按照krato的说法(这让它工作起来了),我的应用程序仍然不能完美地工作。即使它第一次登录,我也可以不断刷新,最终会被注销。所以在阅读了这篇文章后,我设置gunicorn只使用1个工人,如下所示:
网页:gunicorn -w 1:应用程序--预加载
这似乎起了作用。

h6my8fg2

h6my8fg23#

只是为那些来这里沉迷于这个问题的人添加一些提示
此问题可能是由于生产环境中的多线程环境引起的
在处理任何请求之前,上下文flak-login查询load_user,因此您必须根据您的生产环境对其进行定制
您必须实现类似下面的内容,这取决于您如何在处理请求上下文之前向flak-login给予用户对象

@login_manager.user_loader
def load_user(id):
    app.logger.debug(f"session {session}")
    if session:
        return User.query.filter_by(id=session['_user_id']).first()

检查_load_user是如何工作的,因为它已被调用以加载current_user https://github.com/maxcountryman/flask-login/blob/main/src/flask_login/login_manager.py#L301
请在文档www.example.com中检查此信息https://flask-login.readthedocs.io/en/latest/#how-it-works
检查这个也许你只需要添加记住我在cookie https://github.com/maxcountryman/flask-login/blob/main/src/flask_login/login_manager.py#L331

相关问题