aiocontextvars
Asyncio support for PEP-567 contextvars backport.
What it is and what it does
aiocontextvars patches the contextvars backport library to add asyncio support for Python 3.5 and 3.6, allowing context variables to be properly inherited and maintained across async task boundaries. On Python 3.7+, it is simply a pass-through to the built-in contextvars module, which already has full asyncio integration.
The package works by monkey-patching asyncio's event loop and task creation to attach and propagate copied contexts. It requires importing the module before creating any event loops or tasks to ensure the patches are applied. On older Python versions, it provides a workaround for the lack of native context parameter support in call_soon() and Task creation, though the implementation differs from Python 3.7's native approach.
Use it for:
- Maintaining request-scoped context (e.g., user ID, request ID) across async task chains on Python 3.5–3.6.
- Migrating legacy asyncio code from Python 3.5–3.6 that relies on context variable inheritance to Python 3.7+.
- Ensuring context isolation between concurrent tasks in older Python versions before native contextvars asyncio support existed.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides asyncio support for the contextvars backport library on Python 3.5 and 3.6, and delegates to the built-in contextvars module on Python 3.7+.
No, unless you are maintaining legacy code on Python 3.5 or 3.6 that requires contextvars asyncio support. The package is abandoned (last release 2019, last commit 2022) and unnecessary on Python 3.7+. Modern Python versions have built-in contextvars with full asyncio support; use the standard library instead.
Install
aiocontextvars on PyPI
pip
pip install aiocontextvarsuv
uv add aiocontextvarspoetry
poetry add aiocontextvarsInstalling aiocontextvars
Before you install
Low install friction with a single pure-Python dependency. However, the package is abandoned—last release was in 2019 and last commit in 2022. On Python 3.7+, it is purely a delegate to the built-in contextvars module, making it unnecessary for modern codebases.
License in practice
BSD license is permissive and imposes no significant restrictions on use or redistribution.
Quickstart
pip install aiocontextvars
import aiocontextvars
import asyncio
from contextvars import ContextVar
var = ContextVar('var')
var.set('value')
async def main():
print(var.get())
asyncio.run(main())
On Python 3.5 and 3.6, must import aiocontextvars before creating event loops or tasks; custom event loop implementations (e.g., uvloop.new_event_loop) will not be patched and won't behave correctly.
Verify before relying
- Whether the package still works reliably with modern asyncio implementations given its age and abandonment status.
- Whether the contextvars asyncio backport issue mentioned in the description has since been resolved in the upstream contextvars package.
Package facts
| License | BSD license (permissive) |
| Python support | supports the current Python release (>=3.5) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — contextvars |
| Maintenance | abandoned — 2,685 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 446,231/month — #6,613 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: aiocontextvars-0.2.2-py2.py3-none-any.whl
Keywords: aiocontextvars
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
contextvarsProvides the PEP 567 contextvars module for…
permissive · top 5,000 on PyPI
types-contextvarsProvides type stubs for Python's contextvars…
permissive · top 15,000 on PyPI
opencensus-contextProvides in-process context propagation for…
permissive · top 1,000 on PyPI
backports.asyncio.runnerProvides the Python 3.11 asyncio.Runner context…
permissive · top 1,000 on PyPI
asyncioThis package is an obsolete backport of…
permissive · top 1,000 on PyPI
taskgroupBackports asyncio.TaskGroup, asyncio.Runner,…
permissive · top 5,000 on PyPI
nest-asyncioPatches asyncio to allow nested event loops,…
permissive · top 1,000 on PyPI
aiomcacheAsyncio-based memcached client providing…
permissive · top 15,000 on PyPI
nest-asyncio2Patches asyncio to allow nested event loops,…
permissive · top 1,000 on PyPI
backports_abcProvides backports of abstract base classes…
permissive · top 15,000 on PyPI