Can SQLAlchemy be configured to be non-blocking?

I'm under the impression that database calls through SQLAlchemy will block and aren't suitable for use in anything other than synchronous code. Am I correct (I hope I'm not!) or is there a way to configure it to be non-blocking?

Answers


You can use SQLA in a non-blocking style using gevent. Here's an example using psycopg2, using psycopg2's coroutine support:

https://bitbucket.org/zzzeek/green_sqla/

I've also heard folks use the same idea with pymysql. As pymysql is in pure Python and uses the sockets library, gevent patches the socket library to be asynchronous.


Have a look at Tornado as they've got some neat non-blocking libraries, particularly tornado.gen.

We use that along with Momoko, a non-blocking psycopg wrapper lib for Tornado. It's been great so far. Perhaps the only drawback is you lose all the model object stuff that SQLAlchemy gives you. Performance is unreal though.


Without the help of greenlet, the answer is no, in the context of asyncio.

However it is possible to use only a part of SQLAlchemy in asyncio. Please find example in the GINO project, where we used only SQLAlchemy core without engine and full execution context to make a simple ORM in asyncio.


Need Your Help

Core Data: Reset to the initial state

iphone iphone-sdk-3.0 core-data

I have an object, I make some changes to it, but I don't want to save them, I want the 'old' values.

Is there a way to get all managed entities from an EntityManager

jpa persistence entity-relationship

I'm setting up a basic test data util and want to keep track of all the data that the EntityManager handles. Rather than just having a bunch of lists for each entity is there a way to grab everything