# StreamingResponse driven by a TaskGroup

**URL:** <https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320>\
**Category:** Async\
**Created:** [April 9, 2025, 5:38am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320 "2025-04-09T05:38:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/graingert/32/31686_2.png) [@graingert](https://forum.djangoproject.com/u/graingert)\
**Post date:** [April 9, 2025, 5:38am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/1 "2025-04-09T05:38:36Z")

</div>

I’d like to be able to return a streaming response driven by a TaskGroup eg:

```auto
async def news_and_weather(request: HttpRequest) -> StreamingHttpResponse:
    async def gen():
        async def push(ws_url: str, tx: MemoryObjectSendStream) -> None:
            async with tx, connect_ws(ws_url) as conn:
                async for msg in conn:
                    await tx.send(msg)

        async with anyio.create_task_group() as tg:
            tx, rx = anyio.create_memory_object_stream[bytes]()
            with tx, rx:
                tg.start_soon(push, "ws://example.com/news", tx.clone())
                tg.start_soon(push, "ws://example.com/weather", tx.clone())
                tx.close()
                async for msg in rx:
                    yield msg # yield in async generator!! illegal inside TaskGroup!
    return StreamingHttpResponse(gen())

```

however this doesn’t work because I’m using a yield inside an async generator that’s not a context manager, and calling aclosing() on that async generator is not sufficient to allow a TaskGroup to cancel itself and catch the cancel error.

I’d like to be able to instead return a new class StreamingCmgrHttpResponse:

```auto
async def news_and_weather(request: HttpRequest) -> StreamingCmgrHttpResponse:
    @contextlib.asynccontextmanager
    async def acmgr_gen():
        async def push(ws_url: str, tx: MemoryObjectSendStream) -> None:
            async with tx, connect_ws(ws_url) as conn:
                async for msg in conn:
                    await tx.send(msg)

        async with anyio.create_task_group() as tg:
            tx, rx = anyio.create_memory_object_stream[bytes]()
            with tx, rx:
                tg.start_soon(push, "ws://example.com/news", tx.clone())
                tg.start_soon(push, "ws://example.com/weather", tx.clone())
                tx.close()
                yield rx # yield inside asynccontextmanager, permitted inside TaskGroup

    return StreamingCmgrHttpResponse(acmgr_gen())

```

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [April 9, 2025, 6:29am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/2 "2025-04-09T06:29:34Z")

</div>

Hi @graingert — what’s the question here?

As long as your class does the right thing and responds `True` to [response.streaming](https://docs.djangoproject.com/en/5.2/ref/request-response/#django.http.StreamingHttpResponse.streaming) you should be good to go no?

ASGIHandler does this:

```auto
            async with aclosing(aiter(response)) as content:
                async for part in content:
                     ...

```

What else would your example need to be able to work?

---

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/graingert/32/31686_2.png) [@graingert](https://forum.djangoproject.com/u/graingert)\
**Post date:** [April 9, 2025, 6:34am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/3 "2025-04-09T06:34:10Z")

</div>

> [@carltongibson](#):
>
> As long as your class does the right thing and responds `True` to [response.streaming](https://docs.djangoproject.com/en/5.2/ref/request-response/#django.http.StreamingHttpResponse.streaming) you should be good to go no?

No this won’t work because there’s nowhere for the asynccontextmanager to collect the asyncio.CancelledError

> [@carltongibson](#):
>
> What else would your example need to be able to work?

the code here: [django/django/core/handlers/asgi.py at a2f7b3a6a04c8c46c38040c9d9d5bdc6298bd714 · django/django · GitHub](https://github.com/django/django/blob/a2f7b3a6a04c8c46c38040c9d9d5bdc6298bd714/django/core/handlers/asgi.py#L331-L342)  
needs an extra something like this:

```auto
        if response.streaming_cmgr:
            # - Consume via ` __aiter__ ` and not `streaming_content` directly, to
            # allow mapping of a sync iterator.
            # - Use aclosing() when consuming aiter. See
            # https://github.com/python/cpython/commit/6e8dcdaaa49d4313bf9fab9f9923ca5828fbb10e
            async with response.streaming_cmgr_content as content:
                async for part in content:
                    for chunk, _ in self.chunk_bytes(part):
                        await send(
                            {
                                "type": "http.response.body",
                                "body": chunk,
                                # Ignore "more" as there may be more parts; instead,
                                # use an empty final closing message with False.
                                "more_body": True,
                            }
                        )

```

we could have sync/async versions of this - but the sync one would be less useful as there’s not an easy way for a sync context manager to raise an exception into its body like an async one can.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [April 9, 2025, 6:40am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/4 "2025-04-09T06:40:43Z")

</div>

Sorry @graingert — it’s early ☕ — I’m not quite following…

> [@graingert](#):
>
> `async with response.streaming_cmgr_content as content:`

All you’ve effectively done there is remove the `aclosing()` no? 🤔 (Assuming `.streaming_cmgr_content` is just tweaked `streaming_content`)

This may go easier if you create a branch on your fork with some test cases showing the behaviour?

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [April 9, 2025, 6:53am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/5 "2025-04-09T06:53:23Z")

</div>

Is the question, “You can’t wrap a task group in an async generator, so can we adjust ASGIHandler to handle task groups?”

If so, yes, probably.

> [@carltongibson](#):
>
> This may go easier if you create a branch on your fork with some test cases showing the behaviour?

That.

---

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/graingert/32/31686_2.png) [@graingert](https://forum.djangoproject.com/u/graingert)\
**Post date:** [April 9, 2025, 7:03am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/6 "2025-04-09T07:03:05Z")

</div>

yep! I’ve got a branch up now

---

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/graingert/32/31686_2.png) [@graingert](https://forum.djangoproject.com/u/graingert)\
**Post date:** [April 9, 2025, 7:10am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/7 "2025-04-09T07:10:43Z")

</div>

well it doesn’t have any test cases yet

---

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/graingert/32/31686_2.png) [@graingert](https://forum.djangoproject.com/u/graingert)\
**Post date:** [April 9, 2025, 10:26am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/8 "2025-04-09T10:26:40Z")

</div>

here it is with just two basic end to end tests, [add support for streaming with TaskGroups by graingert · Pull Request #19364 · django/django · GitHub](https://github.com/django/django/pull/19364/files) probably will need a gzip test and a test with the TestClient too

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [April 9, 2025, 10:28am UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/9 "2025-04-09T10:28:09Z")

</div>

OK, thanks. Let me have a play. 🪀

---

<div class="post-metadata">

**Author:** ![graingert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/graingert/32/31686_2.png) [@graingert](https://forum.djangoproject.com/u/graingert)\
**Post date:** [January 28, 2026, 12:42pm UTC](https://forum.djangoproject.com/t/streamingresponse-driven-by-a-taskgroup/40320/10 "2026-01-28T12:42:42Z")

</div>

I’ve opened a feature request on the feature request github [add support for streaming with TaskGroups · Issue #117 · django/new-features · GitHub](https://github.com/django/new-features/issues/117)
