Can I make Django "greenlet-safe"?

The big picture: I want to use the eventlet in some application that performs asynchronous I / O while working with Django models from the outside. Working with Django is externally simple (see Django: how can I use model classes to interact with my database from outside Django? ), But this is not the main problem.

I assume (I have not tested) that using Django from greens is dangerous. In fact, in the case of psycopg2, this is a warning (see http://www.initd.org/psycopg/docs/advanced.html#support-to-coroutine-libraries ):

Psycopg ties are not green. The thread is safe and cannot be used simultaneously by different green threads.

Django has one db connection per stream (right?), And as such, this can lead to scary scenarios when using them. Is it possible to somehow connect the connection object? Or make it "greenlet-local"?

My motivation is to use the connection pool from eventlet ( http://eventlet.net/doc/modules/db_pool.html ) to speed up my IO related application.

+6
database django concurrency django-models eventlet
source share
1 answer

There are several projects for Django to work well with greenlet. I would look at psycogreen , which uses coroutine support in Psycopg> = 2.2. There is a good blog post about using gevent, gunicorn and psycogreen with Django: http://dbinit.com/blog/going-green/ .

+7
source share

All Articles