Home > Back-end >  How can I force asyncio task to run?
How can I force asyncio task to run?

Time:01-16

I'm noticing that when I spawn an asyncio task using create_task, it's first completing the rest of the logic rather than starting that task. I'm forced to add an await asyncio.sleep(0) to get the task started, which seems a bit hacky and unclean to me.

Here is some example code:

async def make_rpc_calls(...some args...)
  val_1, val_2 = await asyncio.gather(rpc_call_1(...), rpc_call_2(...))
  return process(val_1, val_2)

def some_very_cpu_intensive_function(...some args...):
  // Does a lot of computation, can take 20 seconds to run
  
task_1 = asyncio.get_running_loop().create_task(make_rpc_calls(...))
intensive_result = some_very_cpu_intensive_function(...)
await task_1
process(intensive_result, task_1.result())

Anytime I run the above, it runs the some_very_cpu_intensive_function function before the kicking off the expensive RPCs. The only way I've gotten this to work is to do:

async def make_rpc_calls(...some args...)
  val_1, val_2 = await asyncio.gather(rpc_call_1(...), rpc_call_2(...))
  return process(val_1, val_2)

def some_very_cpu_intensive_function(...some args...):
  // Does a lot of computation, can take 20 seconds to run

task_1 = asyncio.get_running_loop().create_task(make_rpc_calls(...))

await asyncio.sleep(0)

intensive_result = some_very_cpu_intensive_function(...)
await task_1
process(intensive_result, task_1.result())

This feels like a hack to me - I'm forcing the event loop to context switch, and doesn't feel like I'm using the asyncio framework correctly. Is there another way I should be approaching this?

CodePudding user response:

sleep() always suspends the current task, allowing other tasks to run.

Setting the delay to 0 provides an optimized path to allow other tasks to run. This can be used by long-running functions to avoid blocking the event loop for the full duration of the function call.

Source: https://docs.python.org/3/library/asyncio-task.html

  •  Tags:  
  • Related