Async view example with django

Django February 06, 2021 python

async view example with django

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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)