11๐
As of now, DRF doesnโt support async "api views". Here is an open issue (#7260) in the DRF community and it is still in the discussion stage.
But, Django providing a decorator/wrapper which allow us to convert our sync views/function to async using sync_to_async(...)
wrapper.
Example,
@sync_to_async
@api_view(["GET"])
def sample_view(request):
data = get_data()
return Response(data)
Note that, here, sample_view(...)
and get_data(...)
are sync functions.
๐คJPG
5๐
You can do it with adrf
:
pip install adrf
then add it to INSTALLED_APPS
INSTALLED_APPS = [
...
'adrf',
]
import asyncio
from asgiref.sync import sync_to_async
@sync_to_async
def do_a_network_call(some_input):
expensive_result = do_expensive_network_call(some_input)
return expensive_result
# Class Based Views:
from adrf.views import APIView
class AsyncView(APIView):
async def get(self, request):
result = await asyncio.gather(do_a_network_call("some_input"))
return Response({"result": result})
# Function Based Views:
from adrf.decorators import api_view
@api_view(['GET'])
async def async_view(request):
result = await asyncio.gather(do_a_network_call("some_input"))
return Response({"result": result})
- [Django]-Invalid http_host header
- [Django]-Django Aggregation โ Expression contains mixed types. You must set output_field
- [Django]-'EntryPoints' object has no attribute 'get' โ Digital ocean
0๐
I think you can Use this decorator in DRF
import asyncio
from functools import wraps
def to_async(blocking):
@wraps(blocking)
def run_wrapper(*args, **kwargs):
return asyncio.run(blocking(*args, **kwargs))
return run_wrapper
Example of usage
@to_async
@api_view(["GET"])
async def sample_view(request):
...
- [Django]-Django Rest Framework partial update
- [Django]-Error trying to install Postgres for python (psycopg2)
- [Django]-How to check DEBUG true/false in django template โ exactly in layout.html
Source:stackexchange.com