Python 对象';X';已附加到会话';15';(此为';1';)仅适用于使用烧瓶的Gunicorn

Python 对象';X';已附加到会话';15';(此为';1';)仅适用于使用烧瓶的Gunicorn,python,flask,flask-sqlalchemy,gunicorn,whoosh,Python,Flask,Flask Sqlalchemy,Gunicorn,Whoosh,我有一个相当大的应用程序,它使用Flask,Flask SQLAlchemy和Flask Whoosh Alchemy进行搜索。当我在本地运行它时,一切都很好。出于某种原因,当我使用gunicorn部署它时,我会收到SQLAlchemy抱怨会话问题。我得到: Object '<School at 0x7fdc2dfbaa10>' is already attached to session '15' (this is '1') 首先要杀死端口8000上的任何东西,然后我使用4个w

我有一个相当大的应用程序,它使用Flask,Flask SQLAlchemy和Flask Whoosh Alchemy进行搜索。当我在本地运行它时,一切都很好。出于某种原因,当我使用gunicorn部署它时,我会收到SQLAlchemy抱怨会话问题。我得到:

 Object '<School at 0x7fdc2dfbaa10>' is already attached to session '15' (this is '1')
首先要杀死端口8000上的任何东西,然后我使用4个worker和120超时,并将其设置为登录到特定位置

问题的代码只是我在建一所学校

def post(self):
    groups_form = GroupsForm(request.form)
    ok = True
    if not groups_form.kids.data:
        ok = False
    if not groups_form.staff.data:
        ok = False
    groups_form.kids.choices = [(k,'x') for k in groups_form.kids.data]
    groups_form.staff.choices = [(s,'x') for s in groups_form.staff.data]
    if groups_form.validate():
        group = Group()
        group.name = groups_form.name.data
        group.kids = []
        for kid in groups_form.kids.data:
            group.kids.append(Kid.query.get(kid))

        for kid in group.kids:
            kid.current_group = groups_form.name.data
        group.school = g.current_school //This is the line the error is complaining about. 
        group.staff = []
        for user in groups_form.staff.data:
            group.staff.append(User.query.get(user))
        ...

我不完全理解为什么将学校设置为全局变量会让SQLAlchemy抱怨。我还没有找到任何与这个具体问题相关的东西。我觉得这与gunicorn是多线程的这一事实有关

这里的变量
g
是什么?
def post(self):
    groups_form = GroupsForm(request.form)
    ok = True
    if not groups_form.kids.data:
        ok = False
    if not groups_form.staff.data:
        ok = False
    groups_form.kids.choices = [(k,'x') for k in groups_form.kids.data]
    groups_form.staff.choices = [(s,'x') for s in groups_form.staff.data]
    if groups_form.validate():
        group = Group()
        group.name = groups_form.name.data
        group.kids = []
        for kid in groups_form.kids.data:
            group.kids.append(Kid.query.get(kid))

        for kid in group.kids:
            kid.current_group = groups_form.name.data
        group.school = g.current_school //This is the line the error is complaining about. 
        group.staff = []
        for user in groups_form.staff.data:
            group.staff.append(User.query.get(user))
        ...