aiopg icon indicating copy to clipboard operation
aiopg copied to clipboard

Possibly use SQLAlchemy Strategies

Open dstufft opened this issue 9 years ago • 7 comments

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?

dstufft avatar Jan 26 '15 03:01 dstufft

Thank you for link to very interesting project.

I need to dig into alchimia code before making answer.

asvetlov avatar Jan 26 '15 06:01 asvetlov

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 avatar Jan 26 '15 11:01 dstufft

@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

jettify avatar Jan 26 '15 21:01 jettify

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

dstufft avatar Jan 26 '15 21:01 dstufft

@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 avatar Jun 26 '15 11:06 eirnym

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

jettify avatar Jun 26 '15 19:06 jettify

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

RazerM avatar Sep 26 '16 00:09 RazerM