2014-01-05 124 views
3

我正在使用nginx,uwsgi和SQLAlchemy進行安裝。我最近從SQLObject切換到SQLAlchemy,現在我看到了奇怪的隨機錯誤。例如: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 

回答

7

在/thesystem/code/api.py切入點很可能是你打開連接。

這意味着您的文件描述符將在工作中繼承,並且這不適用於sqlalchemy。

添加--lazy-應用程序(懶惰應用=在您的INI配置真)在每個工人加載/thesystem/code/api.py而不是在主加載它,然後調用fork()的

0

除了接受的答案之外,如果您不想(或不能)更改lazy-apps的preforking,例如由於內存使用量的增加或uwsgi reload策略的更改,您可以簡單地重新連接分叉後的數據庫:

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

uwsgi.post_fork_hook = setup_db