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

Uploaded Python 3

File details

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

File metadata

  • Download URL: miniasyncio-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2e89602ab9aa8b4e88aeeea929fbfcd48ffb08c2212f25415421da547f35b5ac
MD5 a23b24ad4bcf7d32329a064e094c1b74
BLAKE2b-256 9feef87cbd980f78578d5d4e91144b604706b9edd9bad1a2915d829f63c72191

See more details on using hashes here.

File details

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

File metadata

  • Download URL: miniasyncio-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0120a34d4b77d561c9daebd37c1168019b45df27306146cebe05eed86dce5aa6
MD5 f19ec019e886bb32b0b5e773697390bf
BLAKE2b-256 f9e1bb140013d28e8b8cb0908f7a5b32dd97b12f4a96450aaa013ecb5cbca31a

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