Consider these two Python snippets:
import asyncio
import time
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
print(f"started at {time.strftime('%X')}")
await say_after(1, 'hello')
await say_after(2, 'world')
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
and this one:
import time
def say_after(delay, what):
time.sleep(delay)
print(what)
def main():
print(f"started at {time.strftime('%X')}")
say_after(1, "hello")
say_after(2, "world")
print(f"finished at {time.strftime('%X')}")
main()
they produce exactly the same output, the flow of main()
pauses after each function call.
So what's the difference between a synchronous flow and an asynchronous one in this example? And what's the point on using async/sync if the main flow is being paused / blocked?