三个生产级别的Django异步实例

文章首发公众号「码农吴先生」, 欢迎订阅关注。html

Django3.0 发布的时候,我尝试着用了下它的异步功能。当时它仅仅添加了对ASGI的支持(可见以前的文章 Django 3.0 异步试用分享,直到Django3.1的发布,才支持了视图和中间件的异步,可是关键的Django ORM层仍是没有异步。Django生态对第三方异步的ORM支持又不是很友好,这就致使不少用户面对Django的异步功能无从下手。python

很过文章在描述Django view 和中间件的异步使用方法时,由于没有ORM的异步,在view中大多数用asyncio.sleep来代替,并无真实的案例。这便进一步致使读者无从下手,认为Django 异步彻底没生产使用价值。这观点彻底是错误的,现阶段Django 的异步功能彻底可用于生成。django

下边是来自Arun Ravindran(<Django设计模式和最佳实践>做者) 的三个生产级别的Django 异步使用案例,供你们参考。json

Django 异步的用例

微服务调用

现阶段,大多数系统架构已经从单一架构进化为微服务架构,在业务逻辑中调用其余服务的接口成为常有的事情。Django 的异步view 在这种状况下,能够很大程度上提升性能。设计模式

让咱们看下做者的例子:经过两个微服务的接口来获取最后展现在home页的数据。api

# 同步版本
def sync_home(request):
    """Display homepage by calling two services synchronously"""
    context = {}
    try:
        # httpx 支持异步http client ,可理解为requests的升级异步版,彻底兼容requests 的api。
        response = httpx.get(PROMO_SERVICE_URL)
        if response.status_code == httpx.codes.OK:
            context["promo"] = response.json()
        response = httpx.get(RECCO_SERVICE_URL)
        if response.status_code == httpx.codes.OK:
            context["recco"] = response.json()
    except httpx.RequestError as exc:
        print(f"An error occurred while requesting {exc.request.url!r}.")
    return render(request, "index.html", context)


# 异步版本
async def async_home_inefficient(request):
    """Display homepage by calling two awaitables synchronously (does NOT run concurrently)"""
    context = {}
    try:
        async with httpx.AsyncClient() as client:
            response = await client.get(PROMO_SERVICE_URL)
            if response.status_code == httpx.codes.OK:
                context["promo"] = response.json()
            response = await client.get(RECCO_SERVICE_URL)
            if response.status_code == httpx.codes.OK:
                context["recco"] = response.json()
    except httpx.RequestError as exc:
        print(f"An error occurred while requesting {exc.request.url!r}.")
    return render(request, "index.html", context)

# 异步升级版
async def async_home(request):
    """Display homepage by calling two services asynchronously (proper concurrency)"""
    context = {}
    try:
        async with httpx.AsyncClient() as client:
            # 使用asyncio.gather 并发执行协程
            response_p, response_r = await asyncio.gather(
                client.get(PROMO_SERVICE_URL), client.get(RECCO_SERVICE_URL)
            )

            if response_p.status_code == httpx.codes.OK:
                context["promo"] = response_p.json()
            if response_r.status_code == httpx.codes.OK:
                context["recco"] = response_r.json()
    except httpx.RequestError as exc:
        print(f"An error occurred while requesting {exc.request.url!r}.")
    return render(request, "index.html", context)

复制代码

同步版本很显然,当有一个服务慢时,总体的逻辑就会阻塞等待。服务的耗时依赖最后返回的那个接口的耗时。安全

再看异步版本,改用了异步http client 调用,这里的写法并不能增长该view 的速度,两个协程并不能同时执行。当一个协查await时,只是将控制器交还回了事件循环,而不是当即执行本view的其余逻辑或协程。对于本view来讲,仍然是阻塞的。markdown

最后看下异步升级版,使用了asyncio.gather ,它会同时执行两个协程,并在他们都完成的时候返回。升级版至关于并发,普通版至关于串行,Arun Ravindran说效率提高了一半(有待验证)。网络

文件提取

当django 视图须要从文件提取数据,来渲染到模板中时。无论是从本地磁盘仍是网络环境,都会是一个潜在的阻塞I/O操做。在阻塞的这段时间内,彻底能够干别的事情。咱们可使用aiofile库来进行异步的文件I/O操做。架构

async def serve_certificate(request):
    timestamp = datetime.datetime.now().isoformat()

    response = HttpResponse(content_type="application/pdf")
    response["Content-Disposition"] = "attachment; filename=certificate.pdf"
    async with aiofiles.open("homepage/pdfs/certificate-template.pdf", mode="rb") as f:
        contents = await f.read()
        response.write(contents.replace(b"%timestamp%", bytes(timestamp, "utf-8")))
    return response
复制代码

此实例,使用了本地的磁盘文件,若是使用网络文件时,记着修改对应代码。

文件上传

文件上传是一个很长的I/O阻塞操做,结合 aiofile的异步写入功能,咱们能够实现高并发的上传功能。

async def handle_uploaded_file(f):
    async with aiofiles.open(f"uploads/{f.name}", "wb+") as destination:
        for chunk in f.chunks():
            await destination.write(chunk)


async def async_uploader(request):
    if request.method == "POST":
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            await handle_uploaded_file(request.FILES["file"])
            return HttpResponseRedirect("/")
    else:
        form = UploadFileForm()
    return render(request, "upload.html", {"form": form})
复制代码

须要注意的是,这绕过了Django的默认文件上传机制,所以须要注意安全隐患。

总结

本文根据Arun Ravindran的三个准生产级别的实例,阐述了Django 现阶段异步的使用。从这些例子当中能够看出,Django 的异步加上一些异步的第三方库,已经彻底能够应用到生产。咱们生产系统的部分性能瓶颈,特别是I/O类型的,能够考虑使用Django 的异步特性来优化一把了。

我是DeanWu,一个努力成为真正SRE的人。


关注公众号「码农吴先生」, 可第一时间获取最新文章。回复关键字「go」「python」获取我收集的学习资料,也可回复关键字「小二」,加我wx,可拉入技术交流群,聊技术聊人生~

相关文章
相关标签/搜索