Async Caching

143 views
Skip to first unread message

Andrew Wang

unread,
Sep 26, 2020, 11:05:53 AM9/26/20
to Django developers (Contributions to Django itself)
Hey guys, I'd like to contribute to the effort to make Django more async capable. I've started to write an aioredis based cache backend based on django-redis, but I noticed the BaseCache in Django is still all synchronous basically.

I was wondering which backends I should make async capable and how would I go about it?

I was thinking instead of creating a new class, we could just add the async methods (e.g. add, get) to BaseClass. And for the FileBased, Dummy, and LocMem backends, the plan would be the same? Problem with those are Python's local file IO is synchronous...

Adam Johnson

unread,
Sep 26, 2020, 5:56:33 PM9/26/20
to django-d...@googlegroups.com
Hi Andrew

I don't believe any work has started on async caching in core. However there is a plan in DEP 9, the document that outlines how asynchronous caching will work, using suffixed methods like get_async() etc. See https://github.com/django/deps/blob/master/accepted/0009-async.rst#caching . This section specifically says that the default implementations will back onto the sync methods by default, so built-in cache backends won't need to all be converted (at once?).

I think a good start would be making your aioredis-based backend in a third party package, using the future naming scheme `*_async`. It could be informative to have a working backend using this interface, and if you encounter any edge cases whilst creating it.

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/68307bf9-e6f4-4dc3-9e0f-d6e660075a85o%40googlegroups.com.


--
Adam

Andrew Godwin

unread,
Sep 26, 2020, 8:38:07 PM9/26/20
to '1337 Shadow Hacker' via Django developers (Contributions to Django itself)
Agreed - there's no work on caching inside Django yet, since the ORM is my next focus, but I would definitely suggest writing a new pluggable third-party backend that somehow provides async versions of the methods.

The main work to do here is to work how quite how possible it is to offer all of them and if everything can be made to work regardless of what mode (sync or async) it's in; hopefully there's a lot less long-lived-connection issues than in the ORM, say.

Andrew

Andrew Wang

unread,
Sep 26, 2020, 11:30:10 PM9/26/20
to Django developers (Contributions to Django itself)
Hey Adam and Andrew,

I can definitely make the naming scheme something like get_async() rather than just get().

> This section specifically says that the default implementations will back onto the sync methods by default, so built-in cache backends won't need to all be converted (at once?).

I'm slightly confused by what you mean (and what the dep means). Are you saying in case someone is using a built-in cache backend like LocMem for local development and then an async third-party cache backend for production, then the code is like:

class BaseCache:
    def get(...):
        NotImplementedError
    async def get_async(...):
        NotImplementedError

class LocMemCache:
    def get(...):
        ...
    async def get_async(...):
        return await asgiref.sync_to_async(self.get)(*args, **kwargs)

>  It could be informative to have a working backend using this interface, and if you encounter any edge cases whilst creating it.

You can view my progress (once midterms are over :P) over here. I'm planning on making the structure very similar to django-redis to make any migration process to an async Django easier. When I compared redis-py to aioredis, there weren't that many differences besides file structure (and passing the event loop around), so hopefully nothing weird pops up. But I will definitely post in this thread or in a ticket (if I do make a PR for BaseCache) if something odd does pop up.

> The main work to do here is to work how quite how possible it is to offer all of them and if everything can be made to work regardless of what mode (sync or async) it's in

In terms of the package or the other built-in backends or just everything in general like the ORM? If in terms of the package, most if not all methods would have to be awaited... I could also just be confusing all these words, so sorry about uhh me I guess!

Also just saw https://forum.djangoproject.com/. Any preference for where future talks should be held: Google or that forum?

Thanks for the suggestions! Have fun with the ORM!
Andrew

Adam Johnson

unread,
Sep 27, 2020, 3:30:56 AM9/27/20
to django-d...@googlegroups.com
I'm slightly confused by what you mean (and what the dep means).

I understand the DEP as saying that we'll implement the async methods in the BaseCache class as backing onto the sync versions:

class BaseCache
    async def get_async(...):
        return await asgiref.sync_to_async(self.get)(*args, **kwargs)

This means that existing cache backends don't all need to be converted in one go.

Also just saw https://forum.djangoproject.com/. Any preference for where future talks should be held: Google or that forum?

Essentially both are equivalent. I think there remain more readers on the mailing list though, which can help if you're discussing bigger picture things.



--
Adam

Andrew Wang

unread,
Sep 27, 2020, 8:03:07 PM9/27/20
to Django developers (Contributions to Django itself)
Cool, thanks for the clarification. One thing I noticed while programming the test cases (so WIP is here: https://github.com/Andrew-Chen-Wang/django-async-redis) is that it's not really fun having to type out get_async since autocomplete. If a dev knows that they'll be using an await, then I think it's best if everything is prefixed with async_* rather than suffixed. Thoughts?

As a side note, the only other change that would need to happen is in django.core.cache close_caches AFAIK.

Andrew 

Andrew Godwin

unread,
Sep 27, 2020, 8:59:52 PM9/27/20
to '1337 Shadow Hacker' via Django developers (Contributions to Django itself)
So the debate between async_foo and foo_async has not really happened yet; I'm really split on the choice. As you say, async_foo allows you to get to the async versions quicker, but foo_async allows you to start your typing with the operation you're doing and then select the variant.

I think right now I would prefer get_async all things considered, since I don't think autocomplete is necessarily easier either way, but I'm open to suggestions.

Andrew

Adam Johnson

unread,
Sep 28, 2020, 6:36:49 AM9/28/20
to django-d...@googlegroups.com
I think right now I would prefer get_async all things considered

Ditto - reading is more important than writing, and starting with the operation means earlier comprehension of the operation.

Also typing-aware autocomplete can notice when you type "await' and prefer autocompleting async methods, though I'm not sure any autocompletion engines have this feature (yet). I'm using TabNine with the Python language server ( https://github.com/palantir/python-language-server ) and this uses at least some typing information to power its autocompletion.



--
Adam
Reply all
Reply to author
Forward
0 new messages