django-allauth icon indicating copy to clipboard operation
django-allauth copied to clipboard

Interest in async support?

Open bufke opened this issue 1 year ago • 4 comments

Hello, Django's async support is decent these days. I'm curious if you would accept a pull request for async support? It would open up a lot of questions, but first I'd like to know if there is any interest at all.

The io used in allauth looks to be ORM queries and some http requests. The potential benefit, in brief, is that async would allow the server administrator to serve more with less system resources. An additional benefit may be that a new django application today could choose to use an async-first approach and anything incompatible becomes harder to work with when extending. As always - thanks for this project - I love the recent updates!

bufke avatar Dec 06 '23 14:12 bufke

Honestly, I don't see this as a priority at this stage. Unfortunately, due to the way Python async is designed (https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/) this would basically boil down to doubling all the interfaces, providing e.g. each adapter method with a sync and async variant. Now, while this certainly can be achieved, I think cleaning up interfaces first, perhaps even reaching a 1.0, before doubling the work is more important.

pennersr avatar Dec 07 '23 11:12 pennersr

I was just about to create this issue. Honestly now I'm stuck, because I need an async version of this.

Retr02332 avatar Dec 12 '23 03:12 Retr02332

@Retr02332 if you are extending allauth in some way to work in an async context, then you need to make use of sync_to_async. If you come up with a common problem and solution, perhaps post here for others.

bufke avatar Dec 13 '23 15:12 bufke

I found a major problem with django-allauth and django 5's support of async views cancellation.

This might interest you all: https://github.com/pennersr/django-allauth/issues/3566

jrobichaud avatar Dec 14 '23 18:12 jrobichaud

See above mentioned -- now closed -- issues. Apparently, people are already running in async contexts. So the question is, what is currently still missing in order to close this issue?

pennersr avatar Feb 02 '24 21:02 pennersr

#3612 got us on our update to 0.60.1/current. We were on 0.54.0 with Django 4.2 running ASGI with no fuss, but not really fully leveraging async for much as other packages also face similar async adoption pains.

Just being compatible enough to run in that space, even if it's spoiling/blocking is enough of a first step, imo. "ASGI compatible but not optimized/recommended" sure seems close if we were running it that way for so long.

bbrowning918 avatar Feb 07 '24 01:02 bbrowning918

what is currently still missing in order to close this issue?

I think the use cases are:

  1. I want to run allauth without error using ASGI. This seems to work fine as far as I'm aware. Any bug related to this should be tracked as it's own issue IMO and is a higher priority.
  2. I want to employ python async code so as to gain the benefits of async. That is web workers which can handle more requests while using fewer system resources. This includes fetching a remote asset (such as openid connect well known), cache, or Django ORM.
  3. I want to customize django allauth. I only use async functions. When I interact with django-allauth, it's harder to work with. This is the life of a Django developer at this time employing async. :upside_down_face:

If I could wave a magic wand and make allauth fully async on all io related functions, I would. It's not easy. Django ORM itself doesn't even utilize database driver async and instead wraps it in sync_to_async which causes it to execute in a thread. It's rather a pain to write async and sync versions of everything. It would be painful to instead convert all io functions to async and tell end users it's a breaking change. I would keep this issue open but consider it a long term task without any urgency.

bufke avatar Feb 07 '24 14:02 bufke

Worth reading: https://forum.djangoproject.com/t/is-dep009-async-capable-django-still-relevant/30132/3

I don't think it makes sense to keep this ticket open, that magic wand simply does not exist. If there are bugs running in async contexts, please file specific issues. As for a complete async rewrite, maybe, someday...

pennersr avatar Jun 21 '24 12:06 pennersr

I’m very realistic about the fact that we may just not be able to write and maintain what are two parallel ORM cores

That's sad to hear, if I understand that correctly, Andrew, who wrote the DEP, is saying it may be unrealistic to ever provide async Django ORM due to a lack of developer time + unwillness to slow down sync calls via an async first approach. While I realize I'm part of the problem, not doing the work myself, that makes me pretty sad about the future of Django.

If Django isn't planning to release true async support, then yes there is limited benefit to adding it here.

Tangent - I updated a little test program - django-async. Anyone asking themselves "Is async django worth it right now?" might want to see my results. I simulated 3ms of network latency to the database. A simple gunicorn with threading server outperforms async django. That said it's hard to test this stuff and your results will vary.

bufke avatar Jun 21 '24 15:06 bufke