mirror of
https://github.com/langgenius/dify.git
synced 2026-11-11 13:51:25 +08:00
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
14 lines
480 B
Python
14 lines
480 B
Python
from collections.abc import Generator
|
|
from contextlib import ExitStack, contextmanager
|
|
from contextvars import Context
|
|
|
|
|
|
@contextmanager
|
|
def use_contextvars(context_vars: Context | None) -> Generator[None]:
|
|
"""Apply captured bindings and restore the caller's values after cleanup."""
|
|
with ExitStack() as stack:
|
|
if context_vars is not None:
|
|
for var, value in context_vars.items():
|
|
stack.callback(var.reset, var.set(value))
|
|
yield
|