Skip to main content

multiloop: Multi-Event-Loop Engine & Concurrency Toolkit for Python 3.14t

CI Python 3.14 License: MIT

中文版 (Chinese)

Table of Contents


Introduction

multiloop is a high-performance multi-event-loop thread pool and Go-level concurrency primitive toolkit designed exclusively for Python 3.14t (Free-Threaded / no-GIL). It features an asyncssh-style top-level API facade, supporting both zero-config top-level function access and explicit thread pool management.


Installation

Prerequisites

  • Python 3.14+: Free-Threaded (no-GIL) build, e.g. 3.14t.
  • Rust stable toolchain: to compile the _multiloop_core C extension from source.

Install with pip

pip install multiloop

Install with uv

uv add multiloop

Build from source

# After cloning, build and install into the current environment with maturin
maturin develop --release

Note: install maturin (pip install maturin or uv tool install maturin) and the Rust stable toolchain (via rustup) first.


Architecture

graph TD
    UserApp[User Application / FastAPI / ASGI 3.0] -->|pool.submit| Scheduler[Round-Robin Scheduler]
    
    subgraph multiloop Core Engine
        Scheduler -->|Round-Robin Notify| W1[Worker Loop Thread 1]
        Scheduler -->|Round-Robin Notify| W2[Worker Loop Thread 2]
        Scheduler -->|Round-Robin Notify| W3[Worker Loop Thread 3]
        
        W1 <-->|Atomic Metrics| RustCore[Rust C-Extension _multiloop_core]
        W2 <-->|Atomic Metrics| RustCore
        W3 <-->|Atomic Metrics| RustCore
    end
    
    subgraph Golang Concurrency Toolkit
        RustCore <--> FastChan[Channel / flume]
        FastChan <--> Select[multiloop.select_channel]
        FastChan <--> Context[AsyncContext]
        RustCore <--> WaitGroup[AsyncWaitGroup]
    end

Core Features

  • ⚡ True Multithreaded Parallelism: breaks the GIL entirely, achieving up to 3.48x+ physical multi-core speedup in Python 3.14t environments.
  • 🎯 Round-Robin Worker Distribution: tasks are pushed into a shared lock-free queue; worker threads pull and execute via a work-stealing model, with wake-up notifications distributed round-robin across worker loops.
  • 🦀 Rust Engine (_multiloop_core): lock-free primitives and the C extension are written in Rust (PyO3 + flume + parking_lot), delivering zero busy-wait (0% CPU idle) and extreme channel throughput.
  • 🚀 asyncssh-Style Top-Level API Facade: minimal API surface such as multiloop.select_channel(...) and multiloop.EventLoopThreadPool, with pool lifecycle managed via async with.
  • 🦫 Golang-Style Concurrency Primitives:
    • Channel (elegant iteration via async for item in ch:)
    • multiloop.select_channel(*channels) (multi-channel select/multiplex)
    • AsyncContext (cross-thread cascading task cancellation and timeout broadcast)
    • AsyncWaitGroup & AsyncOnce & AsyncRWMutex (read-write lock separation)
  • 🌐 High-Performance Web Engines:
    • MultiloopASGIWorker: Mount FastAPI / Starlette / ASGI 3.0 with Lifespan & RFC 6455 WebSockets.
    • MultiloopWSGIWorker: Run synchronous Django / Flask (PEP 3333) with lock-free streaming response channels.
    • multiloop run CLI runner for zero-downtime serving and hot reloading.

Current Status & Known Limits

multiloop is an experimental project for free-threaded CPython (3.14t). Its concurrency core has been through ten systematic audit rounds (R6–R10), each ending with deterministic reproduction tests and stress verification (pytest --count=50) — docs/CONCURRENCY.md is the operating manual. The API is 0.x: expect behavioural refinements between releases. If you build on it, pin the version.

Verified performance (M1 8GB, Python 3.14t)

Workload Single loop multiloop 4-worker Speedup
CPU-bound dispatch (40 × 2M ops) 2.96 s 0.84 s 3.5x
Mixed I/O + CPU (400 tasks, 2 ms sleep + 40k ops) 511 QPS 1259 QPS 2.5x
Pure-I/O ASGI HTTP (500 req, 50 conn) 59.6 QPS 74.7 QPS 1.25x

The engine's value is parallel CPU-bound work across event loops. For pure-I/O HTTP serving, a single asyncio loop (or uvicorn) is the better tool.

Known limits (read before adopting)

Limit Detail Escape hatch
Requires Python 3.14t free-threaded CPython is still experimental (PEP 703) —
Barrier + cancelled party a party cancelled before the round completes leaves the rest parked forever abort()
select_channel arbitration readiness is reported without consuming; heavy contention can delay a specific channel built-in re-register loop
Waiter removal is O(n) cancelling N parked waiters is O(n²) — ~560 ms at 5000 waiters keep party counts realistic
AsyncContext.cancel() cancels the awaiters, not the running pool task (no injection) design tasks to observe their future
CancelScope shield absorbs cancellations already injected before entry; does not defer new ones (unlike trio/anyio) retry-loop pattern (CONCURRENCY.md §2 Pattern 3)
Windows Proactor: exactly one acceptor (shared-listener multi-acceptor hangs) documented behaviour

Examples

1. Top-Level Zero-Config Usage (asyncssh-style)

import asyncio
import multiloop


async def heavy_task(x: int):
    await asyncio.sleep(0.01)
    return x * 2


async def main():
    # async with manages the thread pool lifecycle automatically
    async with multiloop.EventLoopThreadPool() as pool:
        # Submit an async coroutine (shared queue + work-stealing scheduler)
        fut1 = pool.submit(heavy_task, 21)

        # Explicitly target a worker loop (stateful connection affinity)
        fut2 = pool.submit(heavy_task, 21, pin_to=0)  # Output: 42

        # Inspect pool health metrics
        print("Metrics:", pool.get_metrics())

    # Leaving the async with block shuts down gracefully


if __name__ == "__main__":
    asyncio.run(main())

2. Go-Style Channel Iteration and multiloop.select_channel

import asyncio
import multiloop


async def main():
    ch1 = multiloop.Channel()
    ch2 = multiloop.Channel()

    async def producer():
        await ch1.send("Data from Channel 1")
        ch1.close()

    asyncio.create_task(producer())

    # select_channel waits for the first channel that becomes ready
    selected_ch, val = await multiloop.select_channel(ch1, ch2)
    print(f"Received: {val}")


if __name__ == "__main__":
    asyncio.run(main())

3. Concurrent Task Synchronization (Golang sync.WaitGroup style)

import asyncio
import multiloop


async def worker(name: str, wg: multiloop.AsyncWaitGroup):
    try:
        await asyncio.sleep(0.02)  # simulated work
        print(f"worker {name} done")
    finally:
        wg.done()  # decrement the counter on both success and failure


async def main():
    wg = multiloop.AsyncWaitGroup()

    # Dispatch 5 tasks across a 4-thread pool
    async with multiloop.EventLoopThreadPool(num_threads=4) as pool:
        for i in range(5):
            wg.add()  # increment the counter
            pool.submit(worker, f"task-{i}", wg)

        # Block until all tasks finish (counter reaches zero)
        await wg.wait()
        print("all workers finished")


if __name__ == "__main__":
    asyncio.run(main())

4. Structured Concurrency with Timeout Control

import asyncio
import multiloop


async def fetch(name: str, delay: float):
    await asyncio.sleep(delay)  # simulated network latency
    return f"{name}: ok"


async def main():
    try:
        # fail_after sets an overall timeout (0.1 s) for the whole block
        async with multiloop.fail_after(0.1):
            async with multiloop.TaskGroup() as tg:
                # start_soon spawns a child task immediately, returning a TaskHandle
                h1 = tg.start_soon(fetch, "fast", 0.01)
                h2 = tg.start_soon(fetch, "slow", 0.5)

            # All child tasks are guaranteed finished when leaving the block
            print(await h1, "|", await h2)
    except TimeoutError:
        print("timed out: children did not finish within 0.1 s")


if __name__ == "__main__":
    asyncio.run(main())

More runnable examples: examples/


Live Demo

Want to see it running without writing code? multiloop-fastapi-demo is a real FastAPI application served directly by MultiloopASGIWorker — no uvicorn involved. Its page doubles as a live dashboard: every 2 seconds it pulls real metrics from EventLoopThreadPool.get_metrics() and shows which of the 4 event-loop threads handled each request.

git clone https://github.com/hankaihong1/multiloop-fastapi-demo
cd multiloop-fastapi-demo
uv sync
uv run python app.py        # then open http://127.0.0.1:8000

API Reference

Complete API documentation: docs/API.md.

Not sure which primitive to use? See the decision table in docs/CHOOSING.md.


Quality & Tests

# 1. Lint & static checks (0 errors)
uv run ruff check .

# 2. Full automated test suite
uv run pytest

Community Docs


License

MIT License. See LICENSE for details.

Metadata

Release files for multiloop 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for multiloop 0.1.0
File Size Uploaded
multiloop-0.1.0.tar.gz 233.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for multiloop 0.1.0
File
multiloop-0.1.0-cp314-cp314t-win_amd64.whl CPython 3.14 CPython 3.14 free-threading Windows x86-64 Details
multiloop-0.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ x86-64 Details
multiloop-0.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ ARM64 Details
multiloop-0.1.0-cp314-cp314t-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 free-threading macOS 11.0+ ARM64 Details
multiloop-0.1.0-cp314-cp314t-macosx_10_12_x86_64.whl CPython 3.14 CPython 3.14 free-threading macOS 10.12+ x86-64 Details
multiloop-0.1.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details
multiloop-0.1.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ x86-64 Details
multiloop-0.1.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.14 CPython 3.14 Linux glibc 2.17+ ARM64 Details
multiloop-0.1.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details
multiloop-0.1.0-cp314-cp314-macosx_10_12_x86_64.whl CPython 3.14 CPython 3.14 macOS 10.12+ x86-64 Details

Total release size: 5.9 MB

Release files / multiloop-0.1.0.tar.gz

Download URL multiloop-0.1.0.tar.gz
Size 233.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3c7b9660ca57eecefbfb6d2bd23a1a888fa7be0267763d96302a38fd7e8d742d
BLAKE2b-256 checksum
How to use checksums
0a5138ff5d7713e5514bbb4d0fd1c9fdaff7595d31d40e1e8a318c1dcbcbaf3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314t-win_amd64.whl

Download URL multiloop-0.1.0-cp314-cp314t-win_amd64.whl
Size 552.8 kB
Tags CPython 3.14 CPython 3.14 free-threading Windows x86-64
SHA-256 checksum
How to use checksums
75a659b8597e1b2ff9c36cdb19111d67ee5f9f2dd0a8401976d191e5c9cd2dbf
BLAKE2b-256 checksum
How to use checksums
f243dd74780cbb343c223a4ce26a87bb9113a7484aa57c9a9cc90d31acd72577
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL multiloop-0.1.0-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 595.2 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
4a6639087e97208ddb93a89f125e9046160cb4e4625834920a38ce6257d9f78d
BLAKE2b-256 checksum
How to use checksums
20d5cd0287a26d992b8312f5ef24650d9f8cd483852b4b570ba22908e8217780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL multiloop-0.1.0-cp314-cp314t-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 574.4 kB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
6ecf414875735d0ea9e67537877692ad215ea50511aaeee80417235fe741c312
BLAKE2b-256 checksum
How to use checksums
b27995b8f798ce2fe21e39800a980816308955043230c9c52368161d75c14565
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314t-macosx_11_0_arm64.whl

Download URL multiloop-0.1.0-cp314-cp314t-macosx_11_0_arm64.whl
Size 546.4 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
1ab52385d87755fe0a97c0170ddfb7d82daf2438ae7f1b3eeb707af7cc30e7fa
BLAKE2b-256 checksum
How to use checksums
82cbd499c8a2990140d7ac1d0149cb8eacd571095a935054f1a17745a2423f4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314t-macosx_10_12_x86_64.whl

Download URL multiloop-0.1.0-cp314-cp314t-macosx_10_12_x86_64.whl
Size 572.3 kB
Tags CPython 3.14 CPython 3.14 free-threading macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
179310023c239821207707053529d2bf49a2bfa7429a094d1917505a2215c38a
BLAKE2b-256 checksum
How to use checksums
447c67d5c8ad093b965672ceee4d2e8f4e59efe7013bf8612450f9fd76bfd18e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314-win_amd64.whl

Download URL multiloop-0.1.0-cp314-cp314-win_amd64.whl
Size 551.3 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
4c95cd22e88846df84dd39fde37e9d05b63fa0cd6726da7bb6eebb892ada8aa3
BLAKE2b-256 checksum
How to use checksums
6392ea6fc3415865460724f289d404d31ca7d2752c308b58894a64da9be0ff59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL multiloop-0.1.0-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 594.7 kB
Tags CPython 3.14 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
c6ad714afe13a9a69e0cb69364c992b32939753b35a3eca874efcf351d6b60ef
BLAKE2b-256 checksum
How to use checksums
20f6cb41330f9f8545fa5b4e59e65004c9dc7a6c5fd0ad8f7cd41c65473f53c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL multiloop-0.1.0-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 575.1 kB
Tags CPython 3.14 Linux glibc 2.17+ ARM64
SHA-256 checksum
How to use checksums
2d5996bd70bfe474603f05076ce306761145cb2d2ab9fa9232a518eaff4201ed
BLAKE2b-256 checksum
How to use checksums
410a65828dfea17dc444fe94aecd6d03b16b5ec38d80766a9e543cd24785fa46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL multiloop-0.1.0-cp314-cp314-macosx_11_0_arm64.whl
Size 547.9 kB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
f695d73fa7f07057da15bfe3ff655b5b20e74e2842f16b1f37f740265df65bfe
BLAKE2b-256 checksum
How to use checksums
3efef9592f43ca9b256efba952e78396bae42ab15f5851d0e812875a09a34fef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release files / multiloop-0.1.0-cp314-cp314-macosx_10_12_x86_64.whl

Download URL multiloop-0.1.0-cp314-cp314-macosx_10_12_x86_64.whl
Size 573.3 kB
Tags CPython 3.14 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
4debab83b3843e066afae0db68f0726bddea4001ebb8966259218adbd9ce42f2
BLAKE2b-256 checksum
How to use checksums
6e2453568b7b07d3c81b38eb615c5055ec587f575bc9e7012b6c9c2c6a0ac058
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 18, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.3

11 release files

0.1.2

11 release files

0.1.1

11 release files

This release

0.1.0 This release

11 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page