{"id":6507,"library":"aiotask-context","title":"aiotask-context Library","description":"The `aiotask-context` library provides a mechanism to store and retrieve context information within `asyncio.Task` objects. It is analogous to `threading.local` but designed for asynchronous Python applications, allowing for request-scoped or task-scoped data management without explicit parameter passing. The current version is 0.6.1, with a stable but infrequent release cadence.","status":"active","version":"0.6.1","language":"python","source_language":"en","source_url":"https://github.com/python-aiotask-context/aiotask-context","tags":["asyncio","context","task-local storage","concurrency","async"],"install":[{"cmd":"pip install aiotask-context","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"wrong":"from aiotask_context import context","symbol":"ChainMap","correct":"from aiotask_context import ChainMap"},{"symbol":"get","correct":"from aiotask_context import get"},{"symbol":"set","correct":"from aiotask_context import set"}],"quickstart":{"code":"import asyncio\nfrom aiotask_context import context\n\nasync def nested_task(task_id: str):\n    \"\"\"A sub-task that accesses and modifies context.\"\"\"\n    request_id = context.get(\"request_id\")\n    print(f\"Task {task_id}: Retrieved request_id: {request_id}\")\n\n    # Set a task-specific value; this does not affect other tasks' contexts\n    # nor the parent task's context once the child task's context is copied.\n    context.set(\"task_data\", f\"Data from {task_id}\")\n    await asyncio.sleep(0.01) # Simulate async work\n    print(f\"Task {task_id}: Set task_data: {context.get('task_data')}\")\n\nasync def main():\n    print(\"--- aiotask-context Quickstart ---\")\n\n    # Set context in the main task\n    context.set(\"request_id\", \"req-xyz-123\")\n    print(f\"Main task: Initial request_id: {context.get('request_id')}\")\n\n    # Create and run sub-tasks. Each sub-task inherits a *copy* of the parent's context.\n    task1 = asyncio.create_task(nested_task(\"A\"))\n    task2 = asyncio.create_task(nested_task(\"B\"))\n\n    await task1\n    await task2\n\n    # The main task's context remains unchanged by sub-tasks' modifications\n    print(f\"Main task: Final request_id: {context.get('request_id')}\") # Should be 'req-xyz-123'\n    # Task-specific data set in sub-tasks is not accessible here in the main task\n    print(f\"Main task: task_data: {context.get('task_data')}\") # Should be None\n    print(\"This demonstrates task-local context isolation in asyncio.\")\n\nif __name__ == \"__main__\":\n    asyncio.run(main())","lang":"python","description":"This quickstart demonstrates how `aiotask-context` allows setting context variables that are isolated to the current `asyncio.Task`. Sub-tasks inherit a copy of their parent's context, and modifications within a sub-task do not affect the parent or sibling tasks, ensuring task-local storage."},"warnings":[{"fix":"Update your import statement from `from aiotask_context import Context` and remove any instantiation, using `context` directly instead.","message":"The package underwent a significant change in version 0.4.0. Prior to 0.4.0, `Context` was a class that needed to be instantiated (e.g., `c = Context()`). Since 0.4.0, `context` is a pre-instantiated singleton object directly imported (`from aiotask_context import context`).","severity":"breaking","affected_versions":"< 0.4.0"},{"fix":"Always refer to the official repository at `https://github.com/python-aiotask-context/aiotask-context` for the most up-to-date information, issues, and contributions.","message":"The project's GitHub repository and primary maintainership transitioned from `vimeo/aiotask-context` to `python-aiotask-context/aiotask-context`. While the package name on PyPI remains the same, older documentation or references might point to the outdated repository.","severity":"breaking","affected_versions":"All versions"},{"fix":"Understand that `aiotask-context` provides task-local storage, not global or shared state across tasks. If you need to communicate state *back* to a parent task, use standard asyncio mechanisms like `asyncio.Queue` or return values.","message":"Context in `aiotask-context` is strictly task-local. When `asyncio.create_task` is used, the child task inherits a *copy* of the parent's context. Any modifications made by the child task to its context will not reflect in the parent task's context or sibling tasks' contexts.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For new projects on Python 3.7+, consider evaluating `contextvars` from the standard library. If using `aiotask-context`, be aware of its specific implementation and ensure it meets your requirements.","message":"`aiotask-context` is designed for task-scoped data and should not be confused with `contextvars` (PEP 567) introduced in Python 3.7. While both provide context management, `aiotask-context` predates `contextvars` and relies on direct manipulation of `asyncio.Task` internals. `contextvars` offers a more robust and officially supported way for context propagation.","severity":"gotcha","affected_versions":"All versions, especially Python 3.7+"}],"env_vars":null,"search_vec":"'0.6.1':52 'aiotask':2,7 'aiotask-context':1,6 'allow':33 'analog':24 'applic':32 'async':67 'asynchron':30 'asyncio':60 'asyncio.task':20 'cadenc':59 'concurr':66 'context':3,8,17,61 'current':49 'data':42 'design':28 'explicit':45 'inform':18 'infrequ':57 'librari':4,9 'local':64 'manag':43 'mechan':12 'object':21 'paramet':46 'pass':47 'provid':10 'python':31 'releas':58 'request':36 'request-scop':35 'retriev':16 'scope':37,41 'stabl':55 'storag':65 'store':14 'task':40,63 'task-loc':62 'task-scop':39 'threading.local':26 'version':50 'within':19 'without':44","created_at":"2026-04-15T18:30:50.040212+00:00","updated_at":"2026-04-15T19:42:55.735780+00:00","problems":null,"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.6.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/aiotask-context/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["workflow","http-networking","web-framework"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-30","last_verified":"2026-06-30","next_check":"2026-07-30","install_tag":null}}