mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #34118 -- Adopted asgiref coroutine detection shims.
Thanks to Mariusz Felisiak for review.
This commit is contained in:
@@ -28,10 +28,10 @@ class-based view, this means declaring the HTTP method handlers, such as
|
||||
|
||||
.. note::
|
||||
|
||||
Django uses ``asyncio.iscoroutinefunction`` to test if your view is
|
||||
Django uses ``asgiref.sync.iscoroutinefunction`` to test if your view is
|
||||
asynchronous or not. If you implement your own method of returning a
|
||||
coroutine, ensure you set the ``_is_coroutine`` attribute of the view
|
||||
to ``asyncio.coroutines._is_coroutine`` so this function returns ``True``.
|
||||
coroutine, ensure you use ``asgiref.sync.markcoroutinefunction`` so this
|
||||
function returns ``True``.
|
||||
|
||||
Under a WSGI server, async views will run in their own, one-off event loop.
|
||||
This means you can use async features, like concurrent async HTTP requests,
|
||||
|
||||
@@ -312,7 +312,7 @@ If your middleware has both ``sync_capable = True`` and
|
||||
``async_capable = True``, then Django will pass it the request without
|
||||
converting it. In this case, you can work out if your middleware will receive
|
||||
async requests by checking if the ``get_response`` object you are passed is a
|
||||
coroutine function, using ``asyncio.iscoroutinefunction``.
|
||||
coroutine function, using ``asgiref.sync.iscoroutinefunction``.
|
||||
|
||||
The ``django.utils.decorators`` module contains
|
||||
:func:`~django.utils.decorators.sync_only_middleware`,
|
||||
@@ -331,13 +331,13 @@ at an additional performance penalty.
|
||||
|
||||
Here's an example of how to create a middleware function that supports both::
|
||||
|
||||
import asyncio
|
||||
from asgiref.sync import iscoroutinefunction
|
||||
from django.utils.decorators import sync_and_async_middleware
|
||||
|
||||
@sync_and_async_middleware
|
||||
def simple_middleware(get_response):
|
||||
# One-time configuration and initialization goes here.
|
||||
if asyncio.iscoroutinefunction(get_response):
|
||||
if iscoroutinefunction(get_response):
|
||||
async def middleware(request):
|
||||
# Do something here!
|
||||
response = await get_response(request)
|
||||
|
||||
Reference in New Issue
Block a user