So, the current sticking point I have with the async work is middleware - specifically, synchronous middleware.
The design of Django’s “new style” middleware - a callable that calls another callable - means that the context of the middleware has to stay open while the view runs. I have synchronous middleware adapting around asynchronous views just fine, but this does mean we waste a whole synchronous thread per async view call, which defeats the point of having async in the first place, really.
I can’t think of an easy way out of this; so far, the only options I can consider (neither of which are good) are:
- Rewrite all the basic Django middleware to be async and tell people not to use non-async middleware if they want massive parallelism (throwing anyone with non-standard middleware under the bus)
- Somehow pause the sync middleware and suspend execution in a way where we can come back to it (not even sure this is possible)
Alternative suggestions of how to approach this would be most welcome…