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.4.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.4-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: miniasyncio-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 d74f8d23edaedb7c2456e7762ee0c7c430b187e196017439b0c3c24025c5e3e0
MD5 06aaacb2ed390b8514badd2bebc85a12
BLAKE2b-256 e59ed8781efb8edb77261a23e0dfafcd9c2578f23938f6140b7d8f6a3e476d6a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: miniasyncio-0.0.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e298f8f4d9f762f242d89eef89e115a0d6750b487dceabb9ba48c5a78b9e76e7
MD5 8de1066a186ab5fa680e89299fa90058
BLAKE2b-256 86d24e993d21ee8bc2f9fbc5fbb1dc6f8bc82f4e2ece4314ebf47cf0a9768ce0

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