{"id":261,"library":"anyio","title":"AnyIO","description":"AnyIO is a high-level asynchronous networking and concurrency library that runs on top of either asyncio or Trio, implementing Trio-like structured concurrency on both backends. Code written against AnyIO's API runs unmodified on either backend, allowing incremental adoption. Current stable version is 4.13.0, with an active monthly-ish release cadence under Alex Grönholm (agronholm).","status":"active","version":"4.13.0","language":"python","source_language":"en","source_url":"https://github.com/agronholm/anyio","tags":["async","concurrency","asyncio","trio","structured-concurrency","networking","threads","task-group"],"install":[{"cmd":"pip install anyio","lang":"bash","label":"asyncio backend (default)"},{"cmd":"pip install anyio[trio]","lang":"bash","label":"with Trio backend support"},{"cmd":"pip install anyio[trio] pytest-anyio","lang":"bash","label":"with Trio + built-in pytest plugin"}],"dependencies":[{"reason":"Required only for the Trio backend; install via anyio[trio] extra","package":"trio","optional":true},{"reason":"Used for IDNA 2008 hostname resolution on asyncio; installed automatically","package":"idna","optional":true},{"reason":"Backport of PEP 654 ExceptionGroup for Python < 3.11; required if catching exception groups on older Pythons","package":"exceptiongroup","optional":true}],"imports":[{"wrong":"import anyio","symbol":"anyio","correct":"import anyio"}],"quickstart":{"code":"import anyio\n\nasync def fetch(name: str, delay: float) -> None:\n    print(f'{name}: starting')\n    await anyio.sleep(delay)\n    print(f'{name}: done after {delay}s')\n\nasync def main() -> None:\n    # Task group: all tasks run concurrently; exceptions surface as ExceptionGroup\n    async with anyio.create_task_group() as tg:\n        tg.start_soon(fetch, 'task-A', 1.0)\n        tg.start_soon(fetch, 'task-B', 0.5)\n\n    # Timeout via CancelScope\n    with anyio.move_on_after(2.0) as scope:\n        await anyio.sleep(5)\n    if scope.cancelled_caught:\n        print('timed out (moved on)')\n\n    # Run a blocking call in a thread\n    import time\n    result = await anyio.to_thread.run_sync(time.strftime, '%X')\n    print('wall-clock time from thread:', result)\n\nif __name__ == '__main__':\n    # Default backend is asyncio; swap to 'trio' with no code changes\n    anyio.run(main, backend='asyncio')","lang":"python","description":"Demonstrates concurrent task groups, move_on_after timeout scopes, and offloading blocking calls to a worker thread — all backend-agnostic."},"warnings":[{"fix":"Use 'except* ValueError as eg:' on Python 3.11+, or install the 'exceptiongroup' package and import ExceptionGroup from it on Python 3.10.","message":"In AnyIO 4, task groups always raise ExceptionGroup (PEP 654) when any child task raises. The old anyio.ExceptionGroup class was removed. Code catching exceptions from task groups must use 'except*' syntax (Python 3.11+) or the 'exceptiongroup' backport on older Pythons.","severity":"breaking","affected_versions":"<4.0"},{"fix":"Replace 'await tg.spawn(fn, arg)' with the synchronous 'tg.start_soon(fn, arg)' (no await).","message":"TaskGroup.spawn() was removed in AnyIO 4. Calling it raises AttributeError.","severity":"breaking","affected_versions":"<4.0"},{"fix":"Use: from anyio.from_thread import start_blocking_portal\nwith start_blocking_portal() as portal: portal.call(fn)","message":"start_blocking_portal() was moved to anyio.from_thread and must be used as a synchronous context manager. Importing it from the top-level anyio namespace raises ImportError.","severity":"breaking","affected_versions":"<3.0"},{"fix":"Use the generic syntax: create_memory_object_stream[MyType](max_buffer_size=N)","message":"The 'item_type' keyword argument to create_memory_object_stream() is deprecated and will be removed in a future major version.","severity":"deprecated","affected_versions":">=4.0"},{"fix":"Replace cancellable=True with abandon_on_cancel=True.","message":"The 'cancellable' parameter of to_thread.run_sync() is a deprecated alias for 'abandon_on_cancel'. Passing it emits a DeprecationWarning.","severity":"deprecated","affected_versions":">=4.x"},{"fix":"Pass str(anyio_path) to third-party APIs expecting pathlib.Path, or open the file with await anyio_path.open() and hand the file object over.","message":"anyio.Path cannot be substituted for pathlib.Path. APIs expecting a pathlib.Path will reject an anyio.Path instance; convert with str(path) or pass the underlying pathlib.Path via path._path (private, unstable).","severity":"gotcha","affected_versions":"all"},{"fix":"Spawn all concurrent work through anyio.create_task_group().start_soon() and avoid bare asyncio.create_task() inside AnyIO-managed code.","message":"Mixing native asyncio tasks (asyncio.create_task) with AnyIO cancel scopes can cause tasks spawned outside AnyIO to escape cancellation, and await inside a finally block can receive multiple CancelledError instances under scoped cancellation.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"search_vec":"'4.13.0':49 'activ':52 'adopt':44 'agronholm':61 'alex':59 'allow':42 'anyio':1,2,34 'api':36 'async':62 'asynchron':8 'asyncio':19,64 'backend':30,41 'cadenc':57 'code':31 'concurr':11,27,63,68 'current':45 'either':18,40 'group':73 'grönholm':60 'high':6 'high-level':5 'implement':22 'increment':43 'ish':55 'level':7 'librari':12 'like':25 'month':54 'monthly-ish':53 'network':9,69 'releas':56 'run':14,37 'stabl':46 'structur':26,67 'structured-concurr':66 'task':72 'task-group':71 'thread':70 'top':16 'trio':21,24,65 'trio-lik':23 'unmodifi':38 'version':47 'written':32","created_at":"2026-03-28T04:29:32.615959+00:00","updated_at":"2026-04-15T20:19:04.187551+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"4.14.2","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/agronholm/anyio","docs":"https://anyio.readthedocs.io/en/latest/","changelog":"https://anyio.readthedocs.io/en/stable/versionhistory.html","pypi":"https://pypi.org/project/anyio/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-07-03","last_verified":"2026-08-27","next_check":"2026-08-02","install_tag":"verified"}}