Skip to main content

Async Programming

Hacker’s Thousand‑Task Juggler

Imagine you’re a hacker monitoring thousands of servers. Each one sends logs, alerts, or requests. If you handle them sequentially, you’ll drown in delays. But what if you could juggle all tasks at once by switching between them instantly, without waiting? That’s async programming in Python: non‑blocking code that lets you manage massive workloads efficiently.

Async programming is about concurrency without threads. Instead of creating multiple threads or processes, you write code that cooperatively pauses (await) and resumes, allowing other tasks to run in the meantime.


Why Async Programming

  • Blocking vs Non‑Blocking:
    • Blocking → Code waits for a task to finish before moving on.
    • Non‑Blocking → Code yields control, allowing other tasks to run.
  • Asyncio: Python’s built‑in library for asynchronous programming.
  • Await: Keyword that pauses execution until an async task completes.
  • Event Loop: The scheduler that runs async tasks, switching between them.
  • Real‑World Analogy: Like a chef cooking multiple dishes while one simmers, they chop vegetables for another.

Basic Async & Await

import asyncio

async def greet():
    print("Hello, Hacker!")
    await asyncio.sleep(1)
    print("Welcome back!")

asyncio.run(greet())
  • Why? async defines an asynchronous function, await pauses execution, and the event loop resumes when ready.

Running Multiple Tasks

import asyncio

async def task(name, delay):
    print(f"Starting {name}")
    await asyncio.sleep(delay)
    print(f"Finished {name}")

async def main():
    await asyncio.gather(
        task("Hack1", 2),
        task("Hack2", 3),
        task("Hack3", 1)
    )

asyncio.run(main())
  • Why? asyncio.gather() runs tasks concurrently, switching between them efficiently.

Async I/O Example

import asyncio
import aiohttp

async def fetch(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            return await response.text()

async def main():
    urls = ["https://example.com", "https://python.org"]
    results = await asyncio.gather(*(fetch(url) for url in urls))
    for r in results:
        print(len(r))

asyncio.run(main())
  • Why? Async I/O lets you fetch multiple websites simultaneously without blocking.

Real‑World Example

import asyncio

async def handle_client(reader, writer):
    data = await reader.read(100)
    message = data.decode()
    print(f"Received: {message}")
    writer.write(data)
    await writer.drain()
    writer.close()

async def main():
    server = await asyncio.start_server(handle_client, "127.0.0.1", 8888)
    async with server:
        await server.serve_forever()

asyncio.run(main())
  • Why? Async servers handle multiple clients at once without threads, making them scalable and efficient.

The Hacker’s Notebook

  • Async programming enables non‑blocking code, perfect for handling many tasks simultaneously. async defines asynchronous functions, await pauses execution until tasks complete.
  • The event loop orchestrates tasks, switching between them seamlessly. asyncio.gather() runs multiple tasks concurrently, ideal for I/O bound workloads.

Hacker’s Mindset: treat async programming as your task juggler. It lets you scale missions to thousands of operations without drowning in delays.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Jan 3, 2026