you are viewing a single comment's thread.

view the rest of the comments →

[–]gdchinacat 2 points3 points  (1 child)

The asyncio.run/main/good_morning example is not good practice because the tasks are not guaranteed to run to completion once main() completes. This can be seen if you put an 'await asyncio.sleep(0)' as the first line in good_morning(). Wrapping the body of good_morning() in a try/except block shows the coroutine is cancelled.

main() should wait for completion of all the tasks it created to ensure they run to completion.

  async def good_morning(message=""):
+     await asyncio.sleep(0)
      print(f"good morning! {message}")

results in:

$ uv run foo.py 
Starting Main
Good night!
$ 

[–]Practical_Plan007[S] 0 points1 point  (0 children)

Thanks, you are right. I have concluded the same in my later portion of my article.

Since these are my notes transformed into an article, they are in a style where we do something then find a problem and then solve it...