Bases: StringIO
Capture and forward output to a queue.
Writes from other threads (sync code run via asyncio.to_thread) are handed to
the event loop the capture was created on: asyncio queues are not thread-safe.
The redirect this is used with (contextlib.redirect_stdout) is process-global,
so output of other tasks and threads during a run is captured too.
Source code in src/exxec/local_provider/utils.py
| class StreamCapture(io.StringIO):
"""Capture and forward output to a queue.
Writes from other threads (sync code run via `asyncio.to_thread`) are handed to
the event loop the capture was created on: asyncio queues are not thread-safe.
The redirect this is used with (`contextlib.redirect_stdout`) is process-global,
so output of other tasks and threads during a run is captured too.
"""
def __init__(self, original_stream: TextIO, queue: asyncio.Queue[str]) -> None:
super().__init__()
self.original_stream = original_stream
self.queue = queue
try:
self._loop: asyncio.AbstractEventLoop | None = asyncio.get_running_loop()
except RuntimeError:
self._loop = None
self._loop_thread = threading.get_ident()
def _put(self, line: str) -> None:
with contextlib.suppress(asyncio.QueueFull):
self.queue.put_nowait(line)
def write[S: (str, bytes)](self, s: S) -> int:
"""Capture and forward output to a queue."""
text = s if isinstance(s, str) else s.decode()
result = self.original_stream.write(text)
if text:
lines = text.splitlines(keepends=True)
for line in lines:
if not line.strip():
continue
if self._loop is None or threading.get_ident() == self._loop_thread:
self._put(line.rstrip("\n\r"))
else:
with contextlib.suppress(RuntimeError): # loop already closed
self._loop.call_soon_threadsafe(self._put, line.rstrip("\n\r"))
return result
def flush(self) -> None:
"""Flush the stream."""
return self.original_stream.flush()
|
flush
Flush the stream.
Source code in src/exxec/local_provider/utils.py
| def flush(self) -> None:
"""Flush the stream."""
return self.original_stream.flush()
|
write
Capture and forward output to a queue.
Source code in src/exxec/local_provider/utils.py
| def write[S: (str, bytes)](self, s: S) -> int:
"""Capture and forward output to a queue."""
text = s if isinstance(s, str) else s.decode()
result = self.original_stream.write(text)
if text:
lines = text.splitlines(keepends=True)
for line in lines:
if not line.strip():
continue
if self._loop is None or threading.get_ident() == self._loop_thread:
self._put(line.rstrip("\n\r"))
else:
with contextlib.suppress(RuntimeError): # loop already closed
self._loop.call_soon_threadsafe(self._put, line.rstrip("\n\r"))
return result
|