Markus Johansson
Markus Johansson

Reputation: 3773

Random errors with SQLAlchemy

I'm using a setup with nginx, uwsgi and SQLAlchemy. I recently switched from SQLObject and I'm now seeing strange random errors with SQLAlchemy. For instance:

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

or:

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

Is this some kind of behavior in SQLAlchemy which I'm not aware of? Can it be related to multiple processes/threads in uwsgi?

My uwsgi config file looks like this:

[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

Upvotes: 3

Views: 874

Answers (2)

null
null

Reputation: 424

In addition to the accepted answer, if you do not want to (or cannot) change preforking for lazy-apps, because of the increase in memory usage, for instance, or the changes in your uwsgi reload strategy, you can simply reconnect to the database after forking:

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

uwsgi.post_fork_hook = setup_db

Upvotes: 0

roberto
roberto

Reputation: 12953

Very probably you are opening connections in /thesystem/code/api.py entry point.

That means your file descriptors will be inherited in workers and this does not work with sqlalchemy.

Add --lazy-apps (lazy-apps = true in your ini config) to load /thesystem/code/api.py in each worker instead of loading it in the master and then calling fork()

Upvotes: 7

Related Questions