正如@Blusky 的回答中提到的:
异步 API 将存在于 django 3.X 中。 不早于。
如果这不是一个选项,那么答案就是否。
请注意,即使使用 django 3.X 任何 django 代码,访问数据库的不会是异步的,它必须在线程(线程池)中执行
Celery 用于后台任务或延迟任务,但 celery 永远不会返回 HTTP 响应,因为它没有收到它应该响应的 HTTP 请求。 Celery 对异步也不友好。
您可能不得不考虑更改架构/实现。
看看你的整体问题,问问自己你是否真的需要 Django 的异步 API。
此 API 是用于浏览器应用程序还是用于机器对机器应用程序?
您的客户可以使用网络套接字并等待答案吗?
您能否在服务器端分离阻塞和非阻塞部分?
将 django 用于所有非阻塞、周期性/延迟(django + celelry)的所有内容,并使用 Web 服务器插件或 python ASGI 代码或 Web 套接字实现异步部分。
一些想法
使用 Django + nginx nchan(如果你的网络服务器是 nginx)
nchan 链接:https://nchan.io/
您的 API 调用将创建一个任务 ID,启动一个 celery 任务,立即返回任务 ID 或轮询 url。
轮询 URL 将通过例如 nchan 长轮询通道进行处理。
您的客户端连接到与 nchan 长轮询通道相对应的 url,当您完成任务时,celery 将其解除阻塞(10 秒结束)
使用 Django + 一个 ASGI 服务器 + 一个手工编码视图,并使用类似于 nginx nchan 的策略
同上逻辑,不过你不用nginx nchan,而是自己实现
对所有阻塞 url 使用 ASGI 服务器 + 非阻塞框架(或只是一些手动编码的 ASGI 视图),其余部分使用 Django。
他们可能通过数据库、本地文件或本地 http 请求交换数据。
保持阻塞并在您的服务器上抛出足够的工作进程/线程
这可能是最糟糕的建议,但如果只是供个人使用,
并且你知道你将有多少并行请求,然后确保你有足够的 Django 工作人员,这样你就可以承受阻塞。
在这种情况下,您会为每个慢速请求阻止整个 Django 工作人员。
使用网络套接字。例如使用 Django 的频道模块
Websockets 可以使用 django 通道模块 (pip install channels) (https://github.com/django/channels) 的 django 早期版本 (>= 2.2) 实现
您需要一个 ASGI 服务器来为异步部分提供服务。您可以使用例如 Daphne ot uvicorn(频道文档很好地解释了这一点)
附录 2020-06-01:调用同步 django 代码的简单异步示例
以下代码使用了 starlette 模块,因为它看起来非常简单和小巧
miniasyncio.py
import asyncio
import concurrent.futures
import os
import django
from starlette.applications import Starlette
from starlette.responses import Response
from starlette.routing import Route
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pjt.settings')
django.setup()
from django_app.xxx import synchronous_func1
from django_app.xxx import synchronous_func2
executor = concurrent.futures.ThreadPoolExecutor(max_workers=2)
async def simple_slow(request):
""" simple function, that sleeps in an async matter """
await asyncio.sleep(5)
return Response('hello world')
async def call_slow_dj_funcs(request):
""" slow django code will be called in a thread pool """
loop = asyncio.get_running_loop()
future_func1 = executor.submit(synchronous_func1)
func1_result = future_func1.result()
future_func2 = executor.submit(synchronous_func2)
func2_result = future_func2.result()
response_txt = "OK"
return Response(response_txt, media_type="text/plain")
routes = [
Route("/simple", endpoint=simple_slow),
Route("/slow_dj_funcs", endpoint=call_slow_dj_funcs),
]
app = Starlette(debug=True, routes=routes)
例如,您可以使用以下代码运行此代码
pip install uvicorn
uvicorn --port 8002 miniasyncio:app
然后在您的网络服务器上将这些特定的 url 路由到 uvicorn 而不是您的 django 应用程序服务器。