映射
ContextVars
到它们的值。
Context()
creates an empty context with no values in it. To get a copy of the current context use the
copy_context()
函数。
Each thread has its own effective stack of
Context
objects. The
current context
是
Context
object at the top of the current thread’s stack. All
Context
objects in the stacks are considered to be
entered
.
Entering
a context, which can be done by calling its
run()
method, makes the context the current context by pushing it onto the top of the current thread’s context stack.
Exiting
from the current context, which can be done by returning from the callback passed to the
run()
method, restores the current context to what it was before the context was entered by popping the context off the top of the context stack.
Since each thread has its own context stack,
ContextVar
objects behave in a similar fashion to
threading.local()
when values are assigned in different threads.
Attempting to enter an already entered context, including contexts entered in other threads, raises a
RuntimeError
.
After exiting a context, it can later be re-entered (from any thread).
Any changes to
ContextVar
values via the
ContextVar.set()
method are recorded in the current context. The
ContextVar.get()
method returns the value associated with the current context. Exiting a context effectively reverts any changes made to context variables while the context was entered (if needed, the values can be restored by re-entering the context).
上下文实现
collections.abc.Mapping
接口。
-
run
(
callable
,
*
args
,
**
kwargs
)
¶
-
Enters the Context, executes
callable(*args, **kwargs)
, then exits the Context. Returns
callable
’s return value, or propagates an exception if one occurred.
范例:
import contextvars
var = contextvars.ContextVar('var')
var.set('spam')
print(var.get()) # 'spam'
ctx = contextvars.copy_context()
def main():
# 'var' was set to 'spam' before
# calling 'copy_context()' and 'ctx.run(main)', so:
print(var.get()) # 'spam'
print(ctx[var]) # 'spam'
var.set('ham')
# Now, after setting 'var' to 'ham':
print(var.get()) # 'ham'
print(ctx[var]) # 'ham'
# Any changes that the 'main' function makes to 'var'
# will be contained in 'ctx'.
ctx.run(main)
# The 'main()' function was run in the 'ctx' context,
# so changes to 'var' are contained in it:
print(ctx[var]) # 'ham'
# However, outside of 'ctx', 'var' is still set to 'spam':
print(var.get()) # 'spam'
-
copy
(
)
¶
-
返回上下文对象的浅拷贝。
-
var
in
context
-
返回
True
若
context
has a value for
var
set; return
False
否则。
-
context[var]
-
Return the value of the
var
ContextVar
variable. If the variable is not set in the context object, a
KeyError
被引发。
-
get
(
var
[
,
default
]
)
¶
-
返回值为
var
if
var
has the value in the context object. Return
default
otherwise. If
default
is not given, return
None
.
-
iter(context)
-
Return an iterator over the variables stored in the context object.
-
len(proxy)
-
Return the number of variables set in the context object.
-
keys
(
)
¶
-
Return a list of all variables in the context object.
-
值
(
)
¶
-
Return a list of all variables’ values in the context object.
-
items
(
)
¶
-
Return a list of 2-tuples containing all variables and their values in the context object.