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)
| 69 | |
| 70 | |
| 71 | def 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 | |
| 99 | def require_context() -> TortoiseContext: |
no test coverage detected
searching dependent graphs…