Skip to content

Yazeedx0/python-advanced-learning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Complete asyncio Learning Path

1. Introduction to asyncio

  • What is it? A built-in Python library (since Python 3.4) that lets you write asynchronous code.

  • Why use it?

    • To handle I/O-bound operations (like networking, files, databases).
    • Improve performance instead of waiting for blocking operations.
    • It’s the foundation of web frameworks (like FastAPI and Aiohttp).

2. Core Concepts

  • Synchronous vs Asynchronous

    • Synchronous: each operation waits for the previous one.
    • Asynchronous: operations run concurrently (interleaved).
  • Coroutines Functions defined with async def.

  • Event Loop The “heart” that runs coroutines.


3. Main Tools in asyncio

  1. async / await

    • async def: define a coroutine.
    • await: wait for a coroutine or task.
    import asyncio
    
    async def hello():
        print("Hello")
        await asyncio.sleep(1)
        print("World")
    
    asyncio.run(hello())
  2. Tasks

    • Run coroutines concurrently.
    async def main():
        task1 = asyncio.create_task(hello())
        task2 = asyncio.create_task(hello())
        await task1
        await task2
    asyncio.run(main())

4. Managing Tasks

  • asyncio.gather() → run multiple coroutines together.
  • asyncio.wait_for() → set a timeout.
  • asyncio.shield() → protect a task from cancellation.

5. Working with I/O

  • asyncio.sleep(): simulate delay.
  • asyncio streams: handle TCP/UDP sockets.
  • aiohttp: HTTP client/server built on asyncio.

6. Concurrency Control

  • Semaphore / Lock: prevent race conditions.
  • Queue: manage task communication between coroutines.

7. Advanced Topics

  • Subprocesses with asyncio.
  • Cancellation & Exception handling.
  • Async context managers (async with).
  • Async iterators (async for).
  • Integration with other libraries (FastAPI, SQLAlchemy async).

8. Hands-on Projects

  1. Simple scraper with aiohttp.
  2. Small chat server using asyncio.
  3. Combine asyncio with FastAPI.
  4. Build a small task scheduler.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages