Merge contextvars and log using the sync logger in a thread pool.
(
self,
meth: Callable[..., Any],
event: str,
args: tuple[Any, ...],
kw: dict[str, Any],
)
| 445 | |
| 446 | # Non-Standard Async |
| 447 | async def _dispatch_to_sync( |
| 448 | self, |
| 449 | meth: Callable[..., Any], |
| 450 | event: str, |
| 451 | args: tuple[Any, ...], |
| 452 | kw: dict[str, Any], |
| 453 | ) -> None: |
| 454 | """ |
| 455 | Merge contextvars and log using the sync logger in a thread pool. |
| 456 | """ |
| 457 | # Capture thread-specific info before handing off to the executor. |
| 458 | thread_token = _ASYNC_CALLING_THREAD.set( |
| 459 | (threading.get_ident(), threading.current_thread().name) |
| 460 | ) |
| 461 | scs_token = _ASYNC_CALLING_STACK.set(sys._getframe().f_back.f_back) # type: ignore[union-attr, arg-type, unused-ignore] |
| 462 | ctx = contextvars.copy_context() |
| 463 | |
| 464 | try: |
| 465 | await asyncio.get_running_loop().run_in_executor( |
| 466 | None, |
| 467 | lambda: ctx.run(lambda: meth(event, *args, **kw)), |
| 468 | ) |
| 469 | finally: |
| 470 | _ASYNC_CALLING_STACK.reset(scs_token) |
| 471 | _ASYNC_CALLING_THREAD.reset(thread_token) |
| 472 | |
| 473 | async def adebug(self, event: str, *args: Any, **kw: Any) -> None: |
| 474 | """ |