{"id":587,"library":"trio","title":"Trio","description":"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.","status":"active","version":"0.33.0","language":"python","source_language":"en","source_url":"https://github.com/python-trio/trio","tags":["async","concurrency","networking","io","structured concurrency","event loop"],"install":[{"cmd":"pip install trio","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary import for accessing Trio's API.","symbol":"trio","correct":"import trio"},{"note":"Used for structured concurrency to manage child tasks.","symbol":"open_nursery","correct":"import trio; async with trio.open_nursery() as nursery:"},{"note":"The entry point to run a Trio async function from synchronous code.","symbol":"run","correct":"import trio; trio.run(async_main_function)"}],"quickstart":{"code":"import trio\nimport os\n\nasync def child_task(name, delay):\n    print(f\"{name}: started! Sleeping for {delay} seconds.\")\n    await trio.sleep(delay)\n    print(f\"{name}: exiting!\")\n\nasync def main():\n    print(\"main: started\")\n    async with trio.open_nursery() as nursery:\n        nursery.start_soon(child_task, \"child1\", 1)\n        nursery.start_soon(child_task, \"child2\", 1)\n    print(\"main: all children exited\")\n\nif __name__ == \"__main__\":\n    # In a real application, you might use an environment variable for configuration\n    # e.g., AUTH_TOKEN = os.environ.get('TRIO_AUTH_TOKEN', '')\n    trio.run(main)","lang":"python","description":"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."},"warnings":[{"fix":"If you relied on the old behavior, use `trio.move_on_at(trio.current_time() + ...)` instead to explicitly set a deadline relative to the current time.","message":"The behavior of `trio.move_on_after` and `trio.fail_after` changed in v0.27.0. Previously, deadlines were set relative to initialization time; they now refer to the time of entering the context manager.","severity":"breaking","affected_versions":">=0.27.0"},{"fix":"Use `pytest.RaisesGroup` and `pytest.RaisesExc` from Pytest as their replacements.","message":"`trio.testing.RaisesGroup` and `trio.testing.Matcher` have been deprecated.","severity":"deprecated","affected_versions":">=0.33.0"},{"fix":"Always use `async with trio.open_nursery() as nursery:` and spawn tasks with `nursery.start_soon()` or `nursery.start()`. The nursery ensures all child tasks complete (or are cancelled) before the `async with` block exits.","message":"Spawning 'fire-and-forget' tasks without a `trio.open_nursery()` can lead to leaked tasks, unhandled exceptions, and difficult program shutdowns. Trio's core principle is structured concurrency, meaning tasks should have a clear parent and lifetime.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Design async functions to be aware of and handle `trio.Cancelled` exceptions, especially when managing resources, to ensure proper cleanup.","message":"Trio's cancellation mechanism is explicit and propagated. Any `await` call to a function in the `trio` namespace acts as a checkpoint and can raise a `trio.Cancelled` exception if an enclosing scope is cancelled. Failure to handle `Cancelled` correctly can lead to resource leaks or unexpected program termination.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that code executed in worker threads periodically checks for a cancellation signal (e.g., via a shared event or flag) and exits gracefully, or uses I/O operations that are themselves interruptible.","message":"Calling long-running, blocking I/O operations inside `trio.run_in_worker_thread` without an explicit exit mechanism for the worker can prevent `Ctrl+C` from cleanly shutting down the application, as Trio's cancellation doesn't directly apply to OS threads.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For async generators, use `@trio.as_safe_channel` (introduced in v0.30.0) to ensure safe and structured behavior. Always design channel usage with clear close semantics.","message":"Iterating over an `@as_safe_channel`-derived `ReceiveChannel` could previously raise `trio.BrokenResourceError` if the channel was closed by another task. While a fix was implemented in v0.32.0, improper handling of async generators and channels remains a potential footgun if not following best practices.","severity":"gotcha","affected_versions":"<0.32.0 (bugfix), all versions (general pattern)"}],"env_vars":null,"last_verified":"2026-05-12T16:21:47.577Z","next_check":"2026-06-26T00:00:00.000Z","problems":[],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"0.33.0","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.27,"mem_mb":8.5,"disk_size":"23.7M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.31,"mem_mb":8.5,"disk_size":"23.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.1,"import_time_s":0.21,"mem_mb":8.5,"disk_size":"24M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.24,"mem_mb":8.5,"disk_size":"24M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.39,"mem_mb":8.9,"disk_size":"26.3M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.45,"mem_mb":8.9,"disk_size":"26.3M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.2,"import_time_s":0.35,"mem_mb":8.9,"disk_size":"27M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.36,"mem_mb":8.9,"disk_size":"27M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.4,"mem_mb":8.8,"disk_size":"17.8M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.36,"mem_mb":8.8,"disk_size":"17.9M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.1,"import_time_s":0.35,"mem_mb":8.8,"disk_size":"18M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.37,"mem_mb":8.8,"disk_size":"18M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.31,"mem_mb":8.8,"disk_size":"17.6M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.34,"mem_mb":8.8,"disk_size":"17.5M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2,"import_time_s":0.34,"mem_mb":8.8,"disk_size":"18M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.37,"mem_mb":8.8,"disk_size":"18M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0.26,"mem_mb":8.3,"disk_size":"23.1M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.28,"mem_mb":8.4,"disk_size":"23.1M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":2.5,"import_time_s":0.26,"mem_mb":8.3,"disk_size":"24M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.25,"mem_mb":8.4,"disk_size":"24M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}