skillfed

aiocontextvars

Asyncio support for PEP-567 contextvars backport.

aiocontextvars v0.2.2 446.2K downloads/30d#6,613 on PyPI56
Permissive license BSD license Abandoned released

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 aiocontextvars

uv

uv add aiocontextvars

poetry

poetry add aiocontextvars

Installing 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

Development Status :: 4 - BetaIntended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseNatural Language :: EnglishProgramming Language :: Python :: 3Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6

Tags

contextvars asyncio supportcontext variables async tasksasyncio context propagationPEP-567 backport asynciotask-local context storageasync context inheritance
async-contextlegacy-python

More Software Development packages