SQLAlchemy随机错误

时间:2014-01-05 11:30:14

标签: python nginx sqlalchemy uwsgi

我正在使用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

2 个答案:

答案 0 :(得分:7)

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

这意味着你的文件描述符将在worker中继承,这对sqlalchemy不起作用。

在每个工作者中添加--lazy-apps(lai-apps = true in in in config)加载/thesystem/code/api.py,而不是将其加载到master中,然后调用fork()

答案 1 :(得分:0)

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

>>> differenceofTwoNumbers()
Price of product: 10
Payment amount: 3
2.0