Django 异步生成器通过流式响应,本篇文章已解决
djagno项目的StreamingHttpResponse只支持响应迭代器,不支持异步生成器的方式去响应数据
默认情况会报错 TypeError: ‘async_generator‘ object is not iterable
Traceback (most recent call last):File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 231, in _get_response_asyncresponse = await wrapped_callback(request, *callback_args, **callback_kwargs)
=File "/usr/local/lib/python3.7/site-packages/django/http/response.py", line 386, in _set_streaming_contentself._iterator = iter(value)
TypeError: 'async_generator' object is not iterable
解决方式:创建一个名为stream的装饰器,它可以与一个协程函数一起使用,使其与Django的StreamingHttpResponse兼容。
下面是一个例子:
1.url.py
# url
path('v1/index', index),
2.view.py
import asyncio
import functools
from django.http import StreamingHttpResponse
def stream(coroutine_function):@functools.wraps(coroutine_function)def wrapper(*args, **kwargs):coroutine = coroutine_function(*args, **kwargs)try:while True:yield asyncio.run(coroutine.__anext__())except StopAsyncIteration:passreturn wrapper@stream
async def chunks():for char in 'Hello, world!':yield charawait asyncio.sleep(1)async def index(request):return StreamingHttpResponse(chunks())
3.安装 nest_asyncio
pip install nest_asyncio
4.在settings.py文件的顶部调用apply(), 添加以下代码
import nest_asyncio
nest_asyncio.apply()
5.接下来就是运行项目,调用接口,实现Django 异步生成器通过流式响应
至此,实现Django 异步生成器通过流式响应
如果需要对异步生成器的内容进行操作,可以以下实现
@stream
async def index():@streamasync def chunks() -> iter:async for event in 返回异步生成器方法:yield eventfor i in chunks():# 对chunk进行操作print(i)yield idef index(request):return StreamingHttpResponse(index())