Async view example with django

Django -- Posted on Feb. 6, 2021

async view example with django

                from asgiref.sync import sync_to_async
from django.shortcuts import render

def _get_data(name):
    data = []
    for i in range(0,100000):
        data.append(f'{i} - {name}')
    return data

get_data = sync_to_async(_get_data, thread_sensitive=True)

async def home(request):
    context = {}
    data = get_data('hello word')
    context['data'] = await data
    return render(request,'home.html', context)

Related Posts