I'm trying to call a callback once a async function is done running
Here's an example of what im trying to do:
import asyncio
async def asyncfunction():
print('Hello')
await asyncio.sleep(10)
print('World')
return 10
def callback(n):
print(f'The async function returned: {n}')
loop = asyncio.get_event_loop()
# Will block the print until everything is done
callback(loop.run_until_complete(asyncfunction()))
print('Hey')
Here's what that does:
Hello
World
The async function returned: 10
Hey
And here's what I want it to do
Edit: The position of the 'Hey' doesn't really matter, as long as it doesn't have to wait for the async function to be done
Hello
Hey
World
The async function returned: 10
Edit: after some testing I have found a way that does what I want, although I don't know if its the best way to do it
import asyncio
import threading
async def asyncfunction():
print('Hello')
await asyncio.sleep(10)
print('World')
return 10
def callback(n):
print(f'The async function returned: {n}')
def wrapper(loop):
callback(loop.run_until_complete(asyncfunction()))
loop = asyncio.get_event_loop()
thr = threading.Thread(target=wrapper,args=(loop,))
thr.start()
print('Hey')