thread_scoped_output.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Thread-scoped stdout/stderr silencing for background worker threads.
  2. ``contextlib.redirect_stdout`` reassigns the *process-global* stream, so a daemon worker
  3. silencing itself also silences every other thread (gateway event loop included). This
  4. module installs a per-thread routing proxy as ``sys.stdout``/``sys.stderr``: silenced
  5. threads write to a sink, everyone else passes through to the original stream. Installed
  6. once, idempotently, and never uninstalled (that would race other threads mid-write).
  7. """
  8. from __future__ import annotations
  9. import contextlib
  10. import os
  11. import sys
  12. import threading
  13. from typing import Iterator, TextIO
  14. __all__ = ["thread_scoped_silence"]
  15. _install_lock = threading.Lock()
  16. # Proxy installed per attribute ("stdout"/"stderr"): never double-wrap.
  17. _installed: dict[str, _ThreadRoutingStream] = {}
  18. # One process-lifetime sink per stream: global redirects that displace and
  19. # restore a proxy must not leak a new /dev/null descriptor each time.
  20. _sinks: dict[str, TextIO] = {}
  21. _routing_states: dict[str, _RoutingState] = {}
  22. class _RoutingState:
  23. """Silencing registry shared by every proxy generation for one stream."""
  24. def __init__(self, sink: TextIO) -> None:
  25. self.sink = sink
  26. self.silenced: dict[int, int] = {}
  27. self.lock = threading.Lock()
  28. class _ThreadRoutingStream:
  29. """``sys.stdout``/``sys.stderr`` stand-in routing writes per calling thread;
  30. unknown attributes delegate to the current thread's target."""
  31. def __init__(self, passthrough: TextIO, state: _RoutingState) -> None:
  32. self._passthrough = passthrough
  33. self._state = state
  34. def _target(self) -> TextIO:
  35. return self._state.sink if self._state.silenced.get(threading.get_ident(), 0) > 0 else self._passthrough
  36. def silence(self, ident: int) -> None:
  37. with self._state.lock:
  38. self._state.silenced[ident] = self._state.silenced.get(ident, 0) + 1
  39. def unsilence(self, ident: int) -> None:
  40. with self._state.lock:
  41. depth = self._state.silenced.get(ident, 0) - 1
  42. if depth > 0:
  43. self._state.silenced[ident] = depth
  44. else:
  45. self._state.silenced.pop(ident, None)
  46. def _forward(self, name: str, fallback, *args): # type: ignore[no-untyped-def]
  47. """Call ``name`` on the current target; a dead target yields ``fallback(*args)`` instead of raising."""
  48. try:
  49. return getattr(self._target(), name)(*args)
  50. except Exception:
  51. return fallback(*args)
  52. def write(self, data): # type: ignore[no-untyped-def]
  53. return self._forward("write", lambda d: len(d) if isinstance(d, str) else 0, data)
  54. def flush(self): # type: ignore[no-untyped-def]
  55. return self._forward("flush", lambda: None)
  56. def writelines(self, lines): # type: ignore[no-untyped-def]
  57. return self._forward("writelines", lambda _l: None, lines)
  58. def isatty(self) -> bool:
  59. try:
  60. return bool(self._target().isatty())
  61. except Exception:
  62. return False
  63. def fileno(self): # type: ignore[no-untyped-def]
  64. return self._target().fileno()
  65. def __getattr__(self, name): # type: ignore[no-untyped-def]
  66. return getattr(self._target(), name)
  67. def _ensure_installed(attr: str, passthrough: TextIO) -> _ThreadRoutingStream:
  68. """Install (idempotently) a routing proxy as ``sys.<attr>`` and return it."""
  69. with _install_lock:
  70. proxy = _installed.get(attr)
  71. current = getattr(sys, attr, None)
  72. if isinstance(current, _ThreadRoutingStream):
  73. # A redirect context may restore an older proxy; adopt it rather
  74. # than wrapping it into an unbounded chain.
  75. _installed[attr] = current
  76. _routing_states[attr] = current._state
  77. return current
  78. if proxy is not None and current is proxy:
  79. return proxy
  80. # Route non-silenced threads to whatever is currently bound (an active
  81. # global redirect keeps its old behavior).
  82. passthrough = current if current is not None else passthrough
  83. sink = _sinks.get(attr)
  84. if sink is None or sink.closed:
  85. sink = _sinks[attr] = open(os.devnull, "w", encoding="utf-8")
  86. state = _routing_states.get(attr)
  87. if state is None or state.sink is not sink:
  88. state = _routing_states[attr] = _RoutingState(sink)
  89. proxy = _installed[attr] = _ThreadRoutingStream(passthrough, state)
  90. setattr(sys, attr, proxy)
  91. return proxy
  92. @contextlib.contextmanager
  93. def thread_scoped_silence() -> Iterator[None]:
  94. """Silence ``stdout``/``stderr`` for the *current thread only*."""
  95. ident = threading.get_ident()
  96. proxies = (_ensure_installed("stdout", sys.__stdout__ or sys.stdout), _ensure_installed("stderr", sys.__stderr__ or sys.stderr))
  97. for proxy in proxies:
  98. proxy.silence(ident)
  99. try:
  100. yield
  101. finally:
  102. for proxy in proxies:
  103. proxy.unsilence(ident)