django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.01k stars 793 forks source link

Use the async session.asave method if it exists #2092

Open bigfootjon opened 2 months ago

bigfootjon commented 2 months ago

Support was added in https://github.com/django/django/pull/17372 (slated for release in 5.1)

We can use this API if it exists, otherwise, we fallback to a sync_to_async bridge.

This does break backwards compatibility, which I'm not terribly happy about but I'm not sure how best to fix it.

If users overrode save_session in a subclass they would expect that to still work, so I thought the best way to do this was to make it async in the base class and then await it, so at least it will be obvious that it doesn't work (python will raise an exception about awaiting a non-awaitable), but I'm open to other solutions.

bigfootjon commented 2 months ago

@carltongibson: I've added the missing test coverage (such as I could, see inline comment) and fixed the thing you noted.

I'm not sure this should be merged before https://github.com/django/channels/pull/2090 is resolved since sessions can use the Django ORM under the hood.

carltongibson commented 2 months ago

Yes, I too was thinking we need to settle that first 👍