-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsyncCacheRegion #199
Comments
should we create a package with sqlalchemy async adapted implementation? Or ctrl-c ctrl-v is better in this case? |
yeah...I think for now we would vendor it (the latter option). dogpile.cache is python 3 only now so it can be more succinct. if we put out "sqlalchemy/greenlet_async" then we have to support that separately, would rather not go there yet. |
Hi - am using async SQLAlchemy with FastAPI happily, and was using dogpile.cache before ported the system to async otherwise. Have been struggling to port dogpile over to async, gotten some things to work partially by hacking around in the internals of SA ORM Session, Dogpile Region etc. One problem is that Could you maybe hint at a the way to go, what would AsyncCacheRegion do? I'd have ok time to work on this on coming days, and pretty well versed with async Python in general, just not familiar with SA code from before so it's been a lot to digest. |
this is specific to SQLAlchemy, so when using event.listen with the asyncsession, follow the guidelines at https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#using-events-with-the-asyncio-extension then, when you are inside the event handler, suppose you are using aiomemcache or something like that. no problem, you can call out to asyncio methods inside the event handler using either the connection-bound run_async() method, documented at https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#using-awaitable-only-driver-methods-in-connection-pool-and-other-events, or more directly, and we havent documented this yet, you can run async defs using await_only, just like: from sqlalchemy import event
from sqlalchemy.util import await_only
engine = create_async_engine(...)
session = AsyncSession(engine)
@event.listens_for(session.sync_session, "before_flush")
def evt(sess, context, objects):
cache_data = await_only(my_async_cache.get("some key"))
So this is a much bigger job and for anyone, requires a lot of cognitive work, like more than I have for the time being for sure :). But the general idea is that it would look a lot like AsyncSession. That is, it has all the methods that Region has, with all the "awaitable" ones set up as "async". then the body of each method calls out to the "sync" method on Region, making use of greenlet_spawn for all IO blocking calls. see https://github.com/sqlalchemy/sqlalchemy/blob/3b4d62f4f72e8dfad7f38db192a6a90a8551608c/lib/sqlalchemy/ext/asyncio/session.py#L188 for an example. |
Thanks a lot - sorry for being unclear, I should have reported what had learned so far, quick comments there. Also, the solution I was using for sync, and have tried to port to async now, is the CachingQuery from https://docs.sqlalchemy.org/en/14/_modules/examples/dogpile_caching/caching_query.html
Understood, I actually got some success hooking
Right, the connection binding in ORM Session is where I got lost, at
Makes sense - I'll give another shot in a few days, am doing some other tasks first but return to this later. |
@zzzeek what's your appetite on publishing a |
my appetite for what I think we're talking about here, would be that we add some additional async examples into https://docs.sqlalchemy.org/en/14/orm/examples.html#module-examples.dogpile_caching and that would be it. |
I think it would mainly be module but it's probably less work to just copy that file in dogpile, since it's not something that changes a lot. |
the important thing about the dogpile example is that it's an example. people who use it are compelled to read the source code and take on at least some degree of responsibility for it. if we add new API then we have to maintain it. |
I'm interested in dogpile.cache, are there any examples of using dogpile.cache in an asynchronous way? |
Since there is now such package, maybe dogpile it could just use it |
adapt the approach taken by SQLAlchemy in https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/util/_concurrency_py3k.py to provide for an AsyncCacheRegion frontend.
backends will as always present a "sync" interface that uses greenlets to adapt to the async backend. backends to start with:
The text was updated successfully, but these errors were encountered: