Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make Django "greenlet-safe"?

The big picture is: I want to use eventlet in some application that does asynchronous IO while working with Django models externally. Working with Django externally is simple (see Django: How can I use my model classes to interact with my database from outside Django?) but it's not the main problem.

I presume (I haven't tested) that using Django from greenlets is dangerous. Actually, in the case of psycopg2, we have this warning (see http://www.initd.org/psycopg/docs/advanced.html#support-to-coroutine-libraries):

Psycopg connections are not green thread safe and can’t be used concurrently by different green threads

In Django there is one db connection per thread (right?) and as such it may lead to scary scenarios when used. Is it possible to provide a connection object manually somehow? Or make it "greenlet-local"?

My motivation is to use connection pool from eventlet (http://eventlet.net/doc/modules/db_pool.html) so that I could speed up my IO-bound application.

like image 360
thinred Avatar asked Mar 08 '11 15:03

thinred


1 Answers

There are some projects out there to make Django work well with greenlet. I would check out psycogreen which uses the coroutines support in Psycopg >= 2.2. There is a good blog post on using gevent, gunicorn, and psycogreen together with Django: http://dbinit.com/blog/going-green/.

like image 121
Mark Lavin Avatar answered Nov 17 '22 12:11

Mark Lavin