MCPcopy
hub / github.com/tortoise/tortoise-orm / set_global_context

Function set_global_context

tortoise/context.py:71–96  ·  view source on GitHub ↗

Set the global fallback context for cross-task access. This is used by RegisterTortoise (FastAPI) where asgi-lifespan runs lifespan in a background task, but requests/tests run in a different task. The global context allows these cross-task scenarios to work without explicit co

(ctx: TortoiseContext)

Source from the content-addressed store, hash-verified

69
70
71def set_global_context(ctx: TortoiseContext) -> None:
72 """
73 Set the global fallback context for cross-task access.
74
75 This is used by RegisterTortoise (FastAPI) where asgi-lifespan runs lifespan
76 in a background task, but requests/tests run in a different task.
77 The global context allows these cross-task scenarios to work without
78 explicit context passing.
79
80 Args:
81 ctx: The TortoiseContext to set as global fallback.
82
83 Raises:
84 ConfigurationError: If a global context is already set. Only one global
85 context can be active at a time. For multiple isolated contexts,
86 use explicit TortoiseContext() without global fallback.
87 """
88 global _global_context
89 if _global_context is not None:
90 raise ConfigurationError(
91 "Global context fallback is already enabled by another Tortoise.init() call. "
92 "Only one global context can be active at a time. "
93 "Use explicit TortoiseContext() for multiple isolated contexts, "
94 "or set _enable_global_fallback=False for secondary apps."
95 )
96 _global_context = ctx
97
98
99def require_context() -> TortoiseContext:

Callers 1

initMethod · 0.85

Calls 1

ConfigurationErrorClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…