import asyncio
This imports Python's asynchronous I/O library.
The asyncio module provides tools for writing concurrent code using async/await syntax.
You’ll use it to run asynchronous functions (coroutines).
async def a(): return 1
This defines an asynchronous function (coroutine) named a.
The async def syntax marks this as a coroutine.
When called, it returns a coroutine object, not the value 1 immediately.
Inside, it just returns 1.
async def b(): return await a()
This defines another coroutine b.
Inside it, await a() is used, meaning:
It calls the coroutine a() and waits for it to finish.
It pauses execution until a() completes and returns 1.
Then b() returns that result.
async def c(): return await b()
This is a third coroutine c.
It does the same thing: awaits b(), which awaits a(), which returns 1.
So this creates a simple chain of async calls: c() → b() → a() → 1.
print(asyncio.run(c()))
asyncio.run() is used to run the coroutine c() from synchronous code (like a script).
It:
Starts an event loop.
Runs c() until it completes.
Returns the final result.
So here, it executes c() → b() → a() → returns 1.
That 1 is printed.
Final Output:
1
0 Comments:
Post a Comment