﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33735	Add asynchronous responses for use with an ASGI server	florianvazelle	Carlton Gibson	"This ticket follows the one I opened on [https://github.com/django/daphne/issues/413 django/daphne#413].

==== Initial Issue

Using Daphne as a server for a Django application seems to cause a malfunction in streaming responses.
When you pass an iterator into a response, the contents of the response are not retrieved chunk by chunk, but from a single block when the iterator has finished iterating.
I have a minimal project that can reproduce the behavior, at [https://github.com/florianvazelle/minimal-daphne-stream florianvazelle/minimal-daphne-stream].

==== Solution

After some research, in Daphne, we use Twisted with asyncioreactor. I managed to reproduce the bug in a minimal way: [https://gist.github.com/florianvazelle/c7d8c49fb005a72ed5962ee55a83b000 Minimal example of an ""ASGI-like"" streaming server with Twisted].
The streaming issue occurs when we call a blocking method in an iterator (io, sleep, request ...), which blocks the reactor. That's why the result is not done progressively.
The reactor does not get control of execution back until the end of the iteration.
To correct this behavior we need to add an asynchronous layer and use async / non-blocking alternatives.

==== Proposition

In my minimal project, the view will become:

{{{#!python
import asyncio

from django.http.response import StreamingHttpResponse


async def iterable_content():
    for _ in range(5):
        await asyncio.sleep(1)
        print('Returning chunk')
        yield b'a' * 10000


def test_stream_view(request):
    return StreamingHttpResponse(iterable_content())
}}}

But django does not handle asynchronous generators.
Some works are already did in this tickets [https://code.djangoproject.com/ticket/32798 #32798 (StreamingHttpResponse Raises SynchronousOnlyOperation in ASGI Server) – Django], but considering the performance, we could manage it differently here.

I propose to add asynchronous responses, to be used in an async context, so with an ASGI server.
I make a PoC, in a very basic way: [https://github.com/django/django/pull/15727 PoC Fixed #33735 -- Handle async streaming response]."	New feature	closed	HTTP handling	4.0	Normal	fixed	ASGI async	Carlton Gibson Andrew Godwin Michael Brown Jon Janzen Carles Pina Estany	Ready for checkin	1	0	0	0	0	0
