aiopg
aiopg copied to clipboard
Possibly use SQLAlchemy Strategies
It looks like the aiopg.sa
module relies on reimplementing some of the lower level parts of SQLAlchemy and providing alternatives which use a similar API. I wonder if this could instead be done using SQLAlchemy's support for different execution strategies?
There is an example of doing this for Twisted at https://github.com/alex/alchimia, that defers things to a thread pool, largely because it wants to work for any database not just PostgreSQL. However I wonder if it would be possible to do that instead which might deduplicate some of the code?
Thank you for link to very interesting project.
I need to dig into alchimia code before making answer.
Of course! I could be totally wrong too and the strategy thing might not be useful at all, but figured I'd mention it in case it was useful.
@dstufft interesting point
As far as I can see: sa.Connection
, sa.Transaction
(nested and two phase), sa.ResultProxy
and sa.Engine
must be implemented in both approaches, so there are not much savings.
But I think it would be interesting to add AIOPG_STRATEGY
so end user can write something like:
from sqlalchemy import engine
from aiopg import AIOPG_STRATEGY
@asyncio.coroutine
def go()
engine = yield from create_engine(host, port, strategy=AIOPG_STRATEGY )
in this case DefaultEngineStrategy.create
method must be reimplemented:
[1] https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/engine/strategies.py#L47-L160
By they way, here's some info:
https://twitter.com/zzzeek/status/559747418397634561 https://twitter.com/zzzeek/status/559747644088942592 https://twitter.com/zzzeek/status/559747756563378177
@jettify You can't mix simple function call, generator and async def
from Python3.5, so you must rewrite SQLAlchemy
to use it in async mode.
@eirnym My point was not to rewrite SQLAlchemy
, but use create_engine
from sqlalchemy (not aiopg) in order to create instance of aiopg.Engine
. This is kind of gives user common interface for engine creation.
But now I do not think this is good idea any more.
I created a strategy for asyncio at https://github.com/RazerM/sqlalchemy_aio, based on alchimia. It's currently a proof of concept (lacks tests and documentation).