0

i'm using daphne as a asgi server, with django. turning my view from sync to async was relatively simple, since i could use @sync_to_async at the ORM part. the problem is, some service of mine is like this:

async def service_of_mine(p1, p2, p3):
  r1 = await foo1(p1)
  r2 = await foo2(p2)
  r3 = await foo3(p3)
  return r1, r2, r3

i wanted to run all three calls in parallel, using return await asyncio.gather(foo1(p1), foo2(p2), foo3(p3)) but my api hangs with

Application instance <Task pending name='Task-1' coro=<ASGIStaticFilesHandler.__call__() running at /mypath/python3.10/site-packages/django/contrib/staticfiles/handlers.py:101> wait_for=<Future pending cb=[_chain_future.<locals>._call_check_cancel() at /home/rado/.pyenv/versions/3.10.6/lib/python3.10/asyncio/futures.py:385, Task.task_wakeup()]>> for connection <WebRequest at 0x7f8274b98340 method=GET uri=/myApiPath clientproto=HTTP/1.1> took too long to shut down and was killed.

how can i achieve this?

rado
  • 5,720
  • 5
  • 29
  • 51
  • It's possible the problem isn't on the side of asyncio.gather, but it's simply as the Error Message stated it, and your tasks took too long. Have you tested it using shorter tasks? – Hyalunar Oct 20 '22 at 17:38
  • good point, but i really don't think so, the first piece where i wait synchronously foo3 after foo2 after foo1 is almost instantaneous (these foo* methods are just some rows parsed from database) – rado Oct 20 '22 at 17:59

0 Answers0