Issue
I have the following example code:
from datetime import datetime
import asyncio
async def h():
print("h() has started")
await asyncio.sleep(5)
print("h() has ended")
async def main():
print("{}: start of program".format(datetime.now()))
await h()
print("{}: end of program".format(datetime.now()))
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
I would have expected (and would like the output of code) to be something along the following lines:
2020-03-03 17:31:25.379742: start of program
h() has started
2020-03-03 17:31:30.384977: end of program
h() has ended
however I have the following output:
2020-03-03 17:31:25.379742: start of program
h() has started
h() has ended
2020-03-03 17:31:30.384977: end of program
Is there a specific reason as to why this is happening and how I can go about achieving the desired result?
Solution
The whole point of awaiting a coroutine such as h()
is to wait for it to complete, and (where that makes sense) access its return value. For example, line = await stream.readline()
suspends the current coroutine until readline()
finishes, and then provides the result to assign to line
.
If you want h()
to run in the background, you can use asyncio.create_task
instead. But it still won't result in the desired output, because run_until_complete(main())
only waits for main()
to finish, disregarding additional tasks it may have spawned. (It also returns the return value of the coroutine, much like a sync equivalent of await
.)
If you want to wait for additional tasks, you have to be explicit about it. Perhaps something like:
async def main():
bgtasks = []
print("{}: start of program".format(datetime.now()))
bgtasks.append(asyncio.create_task(h()))
print("{}: end of program".format(datetime.now()))
return bgtasks
async def run_everything():
more_tasks = await main()
await asyncio.gather(*more_tasks)
if __name__ == '__main__':
asyncio.run(run_everything())
Answered By - user4815162342
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.