Invoke a callback with an arbitrary number of parameters. Args: callback: The callable to be invoked. Returns: The return value of the invoked callable.
(callback: Callable[..., Any], *params: object)
| 60 | |
| 61 | |
| 62 | async def invoke(callback: Callable[..., Any], *params: object) -> Any: |
| 63 | """Invoke a callback with an arbitrary number of parameters. |
| 64 | |
| 65 | Args: |
| 66 | callback: The callable to be invoked. |
| 67 | |
| 68 | Returns: |
| 69 | The return value of the invoked callable. |
| 70 | """ |
| 71 | |
| 72 | app: App | None |
| 73 | try: |
| 74 | app = active_app.get() |
| 75 | except LookupError: |
| 76 | # May occur if this method is called outside of an app context (i.e. in a unit test) |
| 77 | app = None |
| 78 | |
| 79 | if app is not None and "debug" in app.features: |
| 80 | # In debug mode we will warn about callbacks that may be stuck |
| 81 | def log_slow() -> None: |
| 82 | """Log a message regarding a slow callback.""" |
| 83 | assert app is not None |
| 84 | app.log.warning( |
| 85 | f"Callback {callback} is still pending after {INVOKE_TIMEOUT_WARNING} seconds" |
| 86 | ) |
| 87 | |
| 88 | call_later_handle = asyncio.get_running_loop().call_later( |
| 89 | INVOKE_TIMEOUT_WARNING, log_slow |
| 90 | ) |
| 91 | try: |
| 92 | return await _invoke(callback, *params) |
| 93 | finally: |
| 94 | call_later_handle.cancel() |
| 95 | else: |
| 96 | return await _invoke(callback, *params) |
no test coverage detected
searching dependent graphs…