Why is my Django view hanging when I use asyncio.gather on a sync_to_async function call, but not when I call it directly?

I have a Django view that calls an asgiref.sync.async_to_sync function that queries a remote API - I'm updating payslip data for a bunch of Employees. Within one of these calls I have to access the DB again so have a asgiref.sync.sync_to_async decorated function to gather that data. I run the Employee update function calls concurrently using asyncio.gather, but this hangs indefinitely and never even enters the DB call - this occurs even if I only have one call in the gather list. Awaiting the function works fine though. Everything is running as an ASGI app under Uvicorn. Any ideas?

Here's a minimal reproduction:

@sync_to_async
def _get_database_employee_payslips(employee_data):
    print(f"Entering DB function")
    return employee_data

@async_to_sync
async def get_full_employees_data(_, __):
    print(f"This works")
    ret = await _get_database_employee_payslips({'id': 1625841})
    print(f"Got {ret}")

    print(f"This doesn't")
    ret = await asyncio.gather(*[
        _get_database_employee_payslips({'id': employee_id})
        for employee_id in [1625841]
    ], return_exceptions=True)    
    print(f"We're hung in the above call")

    return ret

and the results:

INFO 2022-09-27 14:20:20,234 on 10776 140271592661440 Application startup complete.
DEBUG 2022-09-27 14:20:22,163 middleware 10776 140271484180032 process_request
DEBUG 2022-09-27 14:20:22,163 model_factory 10776 140271484180032 No maximum request body size is set, continuing.
DEBUG 2022-09-27 14:20:22,183 model_factory 10776 140271484180032 Created new request model with pk 161dfa90-4082-4ef1-8ab0-84d613c25550
This works
Entering DB function
Got {'id': 1625841}
This doesn't

Environment:

  • WSL2
  • Ubuntu 22.04.1 LTS
  • Python 3.10.4

Python packages (just ones that seem relevant):

aiohttp==3.8.3
aiosignal==1.2.0
anyio==3.6.1
asgiref==3.5.2
async-timeout==4.0.2
asyncio==3.4.3
Django==4.1.1
uvicorn==0.18.3
uvloop==0.17.0
uWSGI==2.0.20

This seems to be a know issue -- nesting async_to_sync and sync_to_async can make the call hang indefinitely, see https://code.djangoproject.com/ticket/32409

Back to Top