Trio

0.33.0 · active · verified Sat Mar 28

Trio is a friendly Python library for async concurrency and I/O, leveraging async/await for structured concurrency. It aims for usability and correctness, providing a simpler yet capable alternative to older async libraries. As of version 0.33.0, it is actively developed with frequent releases, focusing on resilient and predictable async services.

Warnings

Install

Imports

Quickstart

This example demonstrates structured concurrency with `trio.run` and `trio.open_nursery`. It launches two child tasks concurrently, and the `nursery` ensures that `main` waits for both to complete before exiting. Tasks use `trio.sleep` for non-blocking delays.

import trio
import os

async def child_task(name, delay):
    print(f"{name}: started! Sleeping for {delay} seconds.")
    await trio.sleep(delay)
    print(f"{name}: exiting!")

async def main():
    print("main: started")
    async with trio.open_nursery() as nursery:
        nursery.start_soon(child_task, "child1", 1)
        nursery.start_soon(child_task, "child2", 1)
    print("main: all children exited")

if __name__ == "__main__":
    # In a real application, you might use an environment variable for configuration
    # e.g., AUTH_TOKEN = os.environ.get('TRIO_AUTH_TOKEN', '')
    trio.run(main)

view raw JSON →