Threads and requests with Channels 2.0

77 views
Skip to first unread message

Jonathan Stray

unread,
Mar 20, 2018, 6:06:17 PM3/20/18
to Django developers (Contributions to Django itself)
Can anyone offer a brief account of how Channels uses threads to service multiple requests?

My understanding is that there are multiple workers, and I imagine each is a thread which can handle one request (Django view-style request) at a time. Is this correct? I also don't quite understand how this is possible in one Python process given the Global Interpreter Lock.

Basically I am trying to reason about race conditions and scaling, and I don't have enough understanding to do it.

Apologies if this is document elsewhere, but I've so far not been able to find it.

  - Jonathan

Andrew Godwin

unread,
Mar 20, 2018, 6:25:07 PM3/20/18
to Django developers (Contributions to Django itself)
Hi Jonathan,

Daphne runs a single asyncio event loop in the main thread which services all protocol handling (low-level HTTP and websocket) and Channels-level async-native code (ASGI middleware, routing, async consumers). Any synchronous calls (e.g. methods on synchronous consumers) are queued up to run in a threadpool with NUM_CPUS * 5 threads by default.

It's quite different from the thread-handling-one-request model. This isn't the right venue for a complete explanation of how Python async works in combination with the GIL, unfortunately - hopefully you can find that elsewhere if you are interested.

Andrew

--
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-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/b2da063e-0fa0-4f35-a86d-536fe210f1b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages