import asyncio
async def f():
# something, for example await asyncio.sleep(4)
return "return value from f"
async def main():
# schedule f, print result, but don't await
for i in range(10):
await asyncio.sleep(1) # just an example for some other task
print(i)
asyncio.run(main())
I could use print(await f()) but that blocks due to await. I want print to be called as a "callback function" after f() has returned while the rest of main() has already continued. So, assuming that # something in f took 4 seconds, the expected output would look like this:
0
1
2
3
return value from f
4
5
6
7
8
9
add_done_callback,gather, andas_completedcome to mind. Without knowing more specifics, though, it's hard to say which will work best.