SQLAlchemy随机错误

问题描述 投票:3回答:2

我正在使用nginx,uwsgi和SQLAlchemy的设置。我最近从SQLObject切换,现在我看到SQLAlchemy出现奇怪的随机错误。例如:

sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.

要么:

sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'module.id'"

这是SQLAlchemy中的一种我不知道的行为吗?它可以与uwsgi中的多个进程/线程相关吗?

我的uwsgi配置文件如下所示:

[uwsgi]
plugins=python
socket = 127.0.0.1:9002
wsgi-file = /thesystem/code/api.py
master = True
processes  = 4
threads = 2
daemonize = /thesystem/logs/uwsgi.log
pidfile = /thesystem/uwsgi.pid
python nginx sqlalchemy uwsgi
2个回答
7
投票

很可能你是在/ system / code / api.py入口点打开连接。

这意味着您的文件描述符将在worker中继承,这不适用于sqlalchemy。

在你的ini配置中添加--lazy-apps(lazy-apps = true),在每个worker中加载/thesystem/code/api.py,而不是在master中加载它然后调用fork()


0
投票

除了接受的答案之外,如果您不想(或不能)更改延迟应用程序的预制,例如,由于内存使用量的增加,或者uwsgi重新加载策略的更改,您只需重新连接到分叉后的数据库:

import uwsgi
def setup_db():
    """ routine that sets up the connection to your database """
    ...

uwsgi.post_fork_hook = setup_db
© www.soinside.com 2019 - 2024. All rights reserved.