Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 383
Matty Avatar asked Apr 18 '12 16:04

Matty


People also ask

Is SQLAlchemy between inclusive?

Using a SQL BETWEEN operator will evaluate ranges in an inclusive manner. BETWEEN 10 AND 20 is the same as [10, 20]. To write a simple query that represents [10, 20), you can do the following as suggested by Ilja Everilä.

Should I use SQLAlchemy core or ORM?

If you want to view your data in a more schema-centric view (as used in SQL), use Core. If you have data for which business objects are not needed, use Core. If you view your data as business objects, use ORM. If you are building a quick prototype, use ORM.

What is the benefit of SQLAlchemy?

SQLAlchemy is great because it provides a good connection / pooling infrastructure; a good Pythonic query building infrastructure; and then a good ORM infrastructure that is capable of complex queries and mappings (as well as some pretty stone-simple ones).

Is SQLAlchemy worth using?

SQLAlchemy is the ORM of choice for working with relational databases in python. The reason why SQLAlchemy is so popular is because it is very simple to implement, helps you develop your code quicker and doesn't require knowledge of SQL to get started.


3 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.

like image 109
zzzeek Avatar answered Oct 04 '22 21:10

zzzeek


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.

like image 25
kuhnza Avatar answered Oct 04 '22 22:10

kuhnza


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.

like image 45
Fantix King Avatar answered Oct 04 '22 21:10

Fantix King