Very simple code:
async def callee():
await asyncio.sleep(1)
print ("first")
return 1
def caller():
callee() # how do I wait the result of this?
print("second")
async def main():
caller()
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
How do I make this work and print "first" then "second"?
When declaring a async
function and then calling it, also requires to wait for it. Like with the await asyncio.sleep(1)
, you need to await the callee()
. See below:
import asyncio
async def callee():
await asyncio.sleep(1)
print ("first")
return 1
async def caller():
await callee() # how do I wait the result of this?
print("second")
async def main():
await caller()
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.