I'm trying to create an async view that accesses the DB and returns data with two separate queries.
@sync_to_async
def get_tweets(request):
return (Tweet.objects.
annotate(full_text=Concat(F('title'), F('body'))).
order_by(F('date').desc(nulls_last=True)).
values('full_text', 'title', 'body', 'date',
tweet_id=F('uuid'), sent_to_app=F('sent')))
@sync_to_async
def get_users(request):
return User.objects.filter(is_active=True).values('username', 'email',
full_name=Concat(F('first_name'), F('last_name')))
@sync_to_async
@csrf_exempt
@async_to_sync
async def async_view(request):
funcs = [get_tweets, get_users]
tasks = [asyncio.ensure_future(func(request)) for func in funcs]
data = await asyncio.wait(tasks, return_when=FIRST_EXCEPTION)
return JsonResponse({'data': data}, safe=False)
when debugging the code, before the response, I get an exception for every task in tasks:
RuntimeError('Single thread executor already being used, would deadlock')
so I can't collect the results, I don't understand why it's happening except it originated in asgiref.
I downgraded the asgiref version like said here:
but it didn't help. I don't understand if it comes from something in the way I set up the async code or from exception that's being raised in one of the functions and is hidden in the traceback.