I have an async function that is being called (FuncA).
This async function call another async function (FuncB).
Now I'm within an executor, and I want to run FuncB. The executor is synchronous, so can't use wait
.
What I've tried so far:
async def funcB():
# do stuff
pass
async def funcA():
execute(funcB)
# This was called from an Async function (FuncA)
def execute(fn):
# fn is FuncB
result = fn(*self.args, **self.kwargs)
# I can get the current running loop
loop = asyncio.get_running_loop()
# I can create a task
task = asyncio.create_task(result)
# I can't "run until complete" since FuncA is not going to complete so is going to get stuck
loop.run_until_complete(task) # this doesn't work.
# I can't access the task result
task.done() == False
task.result() # Raises InvalidStateError
# I tried making a `regular` loop to wait until it's finished, but it obviously doesn't work since it's blocking
while not task.done(): pass
# Tried running in a Thread, but it won't wait
# Can't use asyncio.run since there's another loop
import threading
x = threading.Thread(target=asyncio.create_task, args=(result,))
x.run()
Running out of ideas. Is this even possible?
I read somewhere (can't find the source) that I can't control with task executes first in the loop (I wanted funcB
to execute immediately, while funcA
will follow it's natural path (whatever that is).