You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I guess this is similar to #357 except it doesn't even require complicated nesting or anything: Calling sync_to_async(..., thread_sensitive=True)-wrapped code from within an async method in a class-based Django view like so
will also cause the wrapped code to run in separate threads:
request handled in thread: 140638173488960
enter f()
sync_to_async()-wrapped f() in thread: 140638133942016
request handled in thread: 140638173488960
enter f()
sync_to_async()-wrapped f() in thread: 140638125287168
exit f()
exit f()
This is despite the docs claiming it should always be a shared thread:
Thread-sensitive mode is quite special, and does a lot of work to run all functions in the same thread. Note, though, that it relies on usage of async_to_sync() above it in the stack to correctly run things on the main thread. If you use asyncio.run() or similar, it will fall back to running thread-sensitive functions in a single, shared thread, but this will not be the main thread.
It's only meant to be the same thread for a given request, because Django sets a different "context" for each request which we respect. Does it do the right thing if called in the same request twice?
Oh I see, thanks!
Yes, it does end up in one and the same thread for calls that happen while handling the same request.
Are there any docs on this I missed? And if not, would it be worth it to open another ticket on Django's tracker about mentioning that it applies per request in the docs I cited?
I don't know if there's docs that you missed that might have indicated it but put it this way - more docs and mentioning it where you read it would be good either way!
Description / example
I guess this is similar to #357 except it doesn't even require complicated nesting or anything: Calling
sync_to_async(..., thread_sensitive=True)
-wrapped code from within anasync
method in a class-based Django view like sowill also cause the wrapped code to run in separate threads:
This is despite the docs claiming it should always be a shared thread:
Minimal reproducible example
A full minimal reproducible example repo demonstrating this can be found here (see there for instructions to reproduce): https://github.com/sh-at-cs/django-sync-to-async-thread-bug
Comments
So I wonder: Is this a bug, did I misread the docs, or should something be changed in them to reflect the actual behavior?
Also let me know if this issue should be addressed to the main Django project instead of asgiref...
Version information
Tested with Django 4.1.5, asgiref 3.6.0.
The text was updated successfully, but these errors were encountered: