Pregunta

I'm using the autobahn server in twisted to provide an RPC API. Some calls require queries to the database and multiple clients may be connected via websocket to the server.

I am using the SqlAlchemy ORM to access the database.

What are the pros and cons of the two following approaches for dealing with SqlAlchemy sessions.

  1. Create and destroy a session for every RPC call
  2. Create a single session when the server starts and use it in every RPC call

Which would you recommend and why? (I'm leaning towards 2)

¿Fue útil?

Solución

The recommended way of doing SQL-based database access from Twisted (and Autobahn) with databases like PostgreSQL, Oracle or SQLite would be twisted.enterprise.adbapi.

twisted.enterprise.adbapi will run queries on a background thread pool, which is required, since most database drivers are blocking.

Sidenote: for PostgreSQL, there is a native-asynchronous, non-blocking driver also: txpostgres.

Now, if you put an ORM like SQLAlchemy on top of the native SQL driver, I'm not sure how this will work together (if at all) with twisted.enterprise.adbapi.

So from the options you mention

  1. Is a no go, since most drivers are blocking (and Autobahn's RPCs run on the main thread = Twisted reactor thread - and you MUST not block that).
  2. With this, you need to put the database session(s) in background threads (again, to not block).

Also see here.

Otros consejos

If you're using SQLAlchemy and Twisted together, consider using Alchimia rather than the built-in adbapi.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top