Share the current request context with the function decorated. The request context is local per task and hence will not be available in any other task. This decorator can be used to make the context available, .. code-block:: python @copy_current_request_context as
(func: Callable)
| 376 | |
| 377 | |
| 378 | def copy_current_request_context(func: Callable) -> Callable: |
| 379 | """Share the current request context with the function decorated. |
| 380 | |
| 381 | The request context is local per task and hence will not be |
| 382 | available in any other task. This decorator can be used to make |
| 383 | the context available, |
| 384 | |
| 385 | .. code-block:: python |
| 386 | |
| 387 | @copy_current_request_context |
| 388 | async def within_context() -> None: |
| 389 | method = request.method |
| 390 | ... |
| 391 | |
| 392 | """ |
| 393 | if not has_request_context(): |
| 394 | raise RuntimeError( |
| 395 | "Attempt to copy request context outside of a request context" |
| 396 | ) |
| 397 | |
| 398 | request_context = _cv_request.get().copy() |
| 399 | |
| 400 | @wraps(func) |
| 401 | async def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 402 | async with request_context: |
| 403 | return await request_context.app.ensure_async(func)(*args, **kwargs) |
| 404 | |
| 405 | return wrapper |
| 406 | |
| 407 | |
| 408 | def copy_current_websocket_context(func: Callable) -> Callable: |
searching dependent graphs…