Skip to main content

aiocontextvars

https://img.shields.io/pypi/v/aiocontextvars.svg https://img.shields.io/travis/fantix/aiocontextvars.svg

IMPORTANT: This package will be deprecated after contextvars asyncio backport is fixed. Before then, this library experimentally provides the missing asyncio support for the contextvars backport library. Please read more in Python 3.7 contextvars documentation.

Compatibility

In Python 3.7 this package is 100% contextvars.

In Python 3.5 and 3.6, this package added asyncio support to the PEP-567 backport package also named contextvars, in a very different way than Python 3.7 contextvars implementation:

  1. call_soon() and family methods.

Python 3.7 added keyword argument context to call_soon() and its family methods. By default those methods will copy (inherit) the current context and run the given method in that context. But aiocontextvars won’t touch the loop, so in order to achieve the same effect, you’ll need to:

loop.call_soon(copy_context().run, my_meth)
  1. Task local.

Python 3.7 used above keyword argument context in Task to make sure that each step of a coroutine is ran in the same context inherited at the time its driving task was created. Meanwhile, aiocontextvars uses Task.current_task() to achieve similar effect: it hacks asyncio and attaches a copied context to the task on its creation, and replaces thread local with current task instance to share the context. This behaves identically to Python 3.7 in most times. What you need to do is to import aiocontextvars before creating loops.

  1. Custom tasks and loops.

Because above hack is done by replacing asyncio.get_event_loop and loop.create_task, therefore tasks and loops created by custom/private API won’t behave correctly as expected, e.g. uvloop.new_event_loop() or asyncio.Task(). Also, event loops created before importing aiocontextvars are not patched either. So over all, you should import aiocontextvars at the beginning before creating event loops, and always use asyncio.* to operate loops/policies, and public asyncio API to create tasks.

Credits

Fantix King is the author and maintainer of this library. This library is open source software under BSD license.

History

0.2.1 (2018-10-24)

  • Changed to single module layout.

  • Updated README.

0.2.0 (2018-09-09)

This is a breaking change. Most implementation is replaced with contextvars. In Python 3.5 and 3.6, aiocontextvars depends on contextvars the PEP-567 backport in PyPI, and patches it to partially support asyncio; in Python 3.7 aiocontextvars is only a delegate to the built-in contextvars library.

  • Modified ContextVar.set() to return a token.

  • Added ContextVar.reset(token).

  • Removed ContextVar.delete().

  • Removed enable_inherit() and disable_inherit(), inherit is always enabled.

  • Added copy_context() and Context.run().

  • Removed Context.current() and Context.inherited.

  • Fixed issue that set_event_loop(None) fails (contributed by J.J. Jackson in #10 #11)

0.1.2 (2018-04-04)

  • Supported Python 3.5.

0.1.1 (2017-12-03)

  • Fixed setup.py

0.1.0 (2017-12-03)

  • First release on PyPI.

Release files for aiocontextvars 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiocontextvars 0.2.2
File Size Uploaded
aiocontextvars-0.2.2.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aiocontextvars 0.2.2
File Interpreter ABI Platform
aiocontextvars-0.2.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 18.5 kB

Release files / aiocontextvars-0.2.2.tar.gz

Download URL aiocontextvars-0.2.2.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f027372dc48641f683c559f247bd84962becaacdc9ba711d583c3871fb5652aa
BLAKE2b-256 checksum
How to use checksums
fbf426986cd042d5f9e4847c8af6274588fae6c9f2ec67e332890572ae9f3bb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

Release files / aiocontextvars-0.2.2-py2.py3-none-any.whl

Download URL aiocontextvars-0.2.2-py2.py3-none-any.whl
Size 4.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
885daf8261818767d8f7cbd79f9d4482d118f024b6586ef6e67980236a27bfa3
BLAKE2b-256 checksum
How to use checksums
dbc17a723e8d988de0a2e623927396e54b6831b68cb80dce468c945b849a9385
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page