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.
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/.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With