Skip to content
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

Fixed #34901 -- Added async-compatible interface to session engines. #17372

Merged
merged 1 commit into from Mar 14, 2024

Conversation

bigfootjon
Copy link
Contributor

@bigfootjon bigfootjon commented Oct 17, 2023

Ticket: https://code.djangoproject.com/ticket/34901
Forum discussions: https://forum.djangoproject.com/t/asyncifying-django-contrib-auth-and-signals-and-maybe-sessions/18770

This adds an async interface and implementation to django.contrib.sessions. This is based on the implementation from Andrew-Chen-Wang which was posted to the Django forum thread about asyncifying contrib modules.

TODO:

  • Tests: I couldn't find any tests of the existing sessions system (besides the tests I updated to be natively async). Where should the test cases for this go?

@timgraham
Copy link
Member

The tests are in sessions_tests.

@bigfootjon
Copy link
Contributor Author

Thanks @timgraham ! I don't know how I missed those. Tests added.

Copy link
Contributor

@Andrew-Chen-Wang Andrew-Chen-Wang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick glance looks great; not sure if ready for review, but thanks for adding my horrible hybrid mess @bigfootjon!

django/contrib/sessions/backends/db.py Show resolved Hide resolved
@bigfootjon
Copy link
Contributor Author

not sure if ready for review

Yep ready for review, thanks for taking a look!

Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bigfootjon Thanks 👍 I left initial comments.

django/contrib/sessions/backends/base.py Outdated Show resolved Hide resolved
django/contrib/sessions/backends/base.py Outdated Show resolved Hide resolved
django/contrib/sessions/backends/base.py Outdated Show resolved Hide resolved
django/contrib/sessions/backends/cache.py Outdated Show resolved Hide resolved
docs/topics/http/sessions.txt Outdated Show resolved Hide resolved
django/contrib/sessions/backends/base.py Show resolved Hide resolved
@bigfootjon
Copy link
Contributor Author

Thanks for the feedback @felixxm! I've made the requested edits

@bigfootjon
Copy link
Contributor Author

@felixxm would you mind taking another look when you get a chance?

@felixxm
Copy link
Member

felixxm commented Mar 12, 2024

Rebased & pushed small edits.


@classmethod
async def aclear_expired(cls):
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you think that we should do the same for file-based backend to avoid context switching?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bigfootjon What do you think? ☝️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an async API for files? My understanding is that Python does not expose an async file io model. IIRC somewhere in Django (ASGI support for static files?) that has a context switch due to this same limitation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured this wasn’t the right vehicle to take on a new dependency, I think aiofiles would be a larger conversation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to suggest that :) I think it's worth adding for consistency and to avoid unnecessary sync_to_async() wrapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure aiofiles is actually what we really want, it just uses a threadpool to do the actual I/O:

aiofiles helps with this by introducing asynchronous versions of files that support delegating operations to a separate thread pool.

(on the main description: https://pypi.org/project/aiofiles/)

This is basically what sync_to_async does so this doesn't seem like it cuts out the context switch :/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems I keep confusing you, sorry for that. I don't want to anything more with the file-base engine. I think we should keep the current versions of async methods (that call sync methods) to avoid sync_to_async() wrapping that is in the base implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh I see what you mean now. Sorry for being dense! I agree!

@felixxm
Copy link
Member

felixxm commented Mar 12, 2024

@bigfootjon Thanks 👍 I've started working on this patch, please push changes (if any) in a separate commits.

@bigfootjon
Copy link
Contributor Author

@bigfootjon Thanks 👍 I've started working on this patch, please push changes (if any) in a separate commits.

Thanks for helping clean it up! I’m traveling right now and won’t be able to contribute for the next 2 weeks, just fyi

@felixxm felixxm force-pushed the ticket_34901 branch 3 times, most recently from 248259c to 61935fb Compare March 13, 2024 09:55
@felixxm
Copy link
Member

felixxm commented Mar 13, 2024

Thanks for helping clean it up! I’m traveling right now and won’t be able to contribute for the next 2 weeks, just fyi

Unfortunately, I only have 2 weeks left 😅

@felixxm
Copy link
Member

felixxm commented Mar 13, 2024

I think it's ready 🎉

@bigfootjon Thanks 👍

@bigfootjon
Copy link
Contributor Author

I think it's ready 🎉

I just replied to one of your inline comments above about using async file io, but other than that I agree! Thanks for pushing this over the line

@felixxm felixxm changed the title Fixed #34901 -- Added async-compatible interface to sessions Fixed #34901 -- Added async-compatible interface to session engines. Mar 13, 2024
Thanks Andrew-Chen-Wang for the initial implementation which was posted
to the Django forum thread about asyncifying contrib modules.
@felixxm felixxm added the selenium Apply to have Selenium tests run on a PR label Mar 13, 2024
@felixxm felixxm merged commit f5c3406 into django:main Mar 14, 2024
39 checks passed
@bigfootjon bigfootjon deleted the ticket_34901 branch March 15, 2024 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
selenium Apply to have Selenium tests run on a PR
Projects
None yet
4 participants