Junior — Middle
Can you tell about the key features and working characteristics of Asyncio in Python?
sobes.tech AI
Answer from AI
Asyncio is a Python library for writing asynchronous code using coroutines, event loop, and non-blocking operations. Main features:
- An event loop manages the execution of tasks and coroutines.
- Coroutines are functions that can be paused and resumed using
asyncandawaitkeywords. - Enables efficient I/O operations without blocking the main thread.
- Supports concurrent execution of tasks, improving performance when working with network operations or files.
Example of simple async code:
import asyncio
async def say_hello():
await asyncio.sleep(1)
print("Hello after 1 second")
async def main():
await asyncio.gather(say_hello(), say_hello())
asyncio.run(main())
Here, two coroutines run in parallel without blocking each other.