Context manager for setting contextvars variables
Project description
This module provides Set, a context manager which sets one or more contextvars variables upon activation and resets them to their previous values at exit.
Usage:
import contextvars, with_contextvars A = contextvars.ContextVar("A") B = contextvars.ContextVar("B") A.set("Hello,") B.set("world!") print(A.get(), B.get()) # prints: Hello, world! with with_contextvars.Set((A, "other"), (B, "value")): print(A.get(), B.get()) # prints: other value print(A.get(), B.get()) # prints: Hello, world!
Even the entirety of variable assignments of a contextvars.Context object (as obtained from contextvars.copy_context()) can be activated by initializing Set with its items:
with with_contextvars.Set(*context.items()): ...
However, using contextvars.Context.run() is more efficient and should be preferred where possible.
More information can be found in the documentation of Set.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
with_contextvars-0.1.0.tar.gz
(3.1 kB
view hashes)
Built Distribution
Close
Hashes for with_contextvars-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78438b01726927af3677834429a4c5e77e5e31e84296c2c5749848c45ed8a145 |
|
MD5 | cf9299fae618656ed28c1d4922e87623 |
|
BLAKE2b-256 | ecfac53471db9b79e3e73bca021ac5446225197b389ffce33fe220422bb1edb6 |