Skip to main content

A small async package

Project description

miniasyncio

A minimal async event loop for Python. Built from scratch to understand how asyncio actually works

Quick Start

import miniasyncio as io

@io.__async__
def greet(name: str):
    print(f"Hello, {name}!")
    yield from io.sleep(0.1).__await__()
    print(f"Done with {name}")

@io.__async__
def main():
    yield from io.gather(
        greet("Alice"),
        greet("Bob"),
        greet("Charlie"),
    ).__await__()

io.run(main())

The equivalent code with asyncio would look like this

import asyncio as io

async def greet(name: str):
    print(f"Hello, {name}!")
    await io.sleep(0.1)
    print(f"Done with {name}")

async def main():
    await io.gather(
        greet("Alice"),
        greet("Bob"),
        greet("Charlie"),
    )

io.run(main())

Output:

Hello, Alice!
Hello, Bob!
Hello, Charlie!
Done with Alice
Done with Bob
Done with Charlie

How It Works

A coroutine is just a generator with extra steps. And await is just a disguised yield!

When you create an async function. Calling your function returns an coroutine object and the function body is not yet executed. The __await__ dunder method on Awaitable objects such as coroutines returns a generator object. A generator allows for a routine to be paused and to yield control to the event loop. This allows the event loop to run another task while another is waiting.

The event loop runs tasks round-robin. When a task awaits, it yields control back to the loop, which runs the next task. This is cooperative multitasking because tasks must await to yield control.

API

Function Description
run(coro) Register a task and run the event loop to completion
create_task(coro) Schedule a coroutine without awaiting
gather(*coros) Await multiple coroutines concurrently, return results as list
sleep(seconds) Non-blocking delay
@__async__ Decorator to make a generator function awaitable
run_loop() Run the event loop manually

Features

  • Event loop — cooperative multitasking with a task queue
  • Coroutines — generator-based async functions via @__async__ decorator
  • Tasks — scheduled coroutines with future-like results
  • gather — run multiple coroutines concurrently
  • sleep — non-blocking delay
  • Zero dependencies — pure Python and standard libraries

Why?

To learn. This reimplements the main concepts of asyncio in a few readable lines so you can see exactly how the event loop works to make async/await feel like magic.

Install

Using pip:

pip install miniasyncio

Using UV:

uv add miniasyncio

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

miniasyncio-0.0.3.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

miniasyncio-0.0.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file miniasyncio-0.0.3.tar.gz.

File metadata

  • Download URL: miniasyncio-0.0.3.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for miniasyncio-0.0.3.tar.gz
Algorithm Hash digest
SHA256 630a110fb71129c61357853c2f2fe3ef2859535741350bc179c8e68dc40e9ab0
MD5 447d9f532e55dcda69d19c6a3e1a9ef0
BLAKE2b-256 f5d27a3f6fae3504503b87b3c5039f7b349ebf6a45c4d6448f02c67c8e67b1bc

See more details on using hashes here.

File details

Details for the file miniasyncio-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: miniasyncio-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for miniasyncio-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 edd67be49b8d7040b92c92633cc63d72ce85f48787ccd2a2332da546ab68d34b
MD5 cfcacd084ed23285531246f38c99c868
BLAKE2b-256 c98602f67cf6fe88431006bea17c8c343740b1336dcdc05c1424d281a2a3a922

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page