stouputils.parallel.capturer module#

class PipeWriter(conn: Any, encoding: str, errors: str)[source]#

Bases: object

A writer that sends data to a multiprocessing Connection.

class CaptureOutput(
encoding: str = 'utf-8',
errors: str = 'replace',
)[source]#

Bases: object

Utility to capture stdout/stderr from a subprocess and relay it to the parent’s stdout.

The class creates an os.pipe(), marks fds as inheritable (for spawn method), provides methods to start a listener thread that reads from the pipe and writes to the main process’s sys.stdout/sys.stderr, and to close/join the listener.

>>> capturer = CaptureOutput(encoding="utf-8", errors="replace")
>>> pass # send capturer object to subprocess
>>> capturer.redirect()  # Redirects sys.stdout/sys.stderr to the pipe
>>> pass # in parent process:
>>> #capturer.parent_close_write()  # Close parent's write end
>>> capturer.start_listener()      # Start listener thread to read from pipe
>>> ...                            # do other work
>>> capturer.join_listener(timeout=0.1)       # Wait for listener to finish (on EOF)
redirect() None[source]#

Redirect sys.stdout and sys.stderr to the pipe’s write end.

parent_close_write() None[source]#

Close the parent’s copy of the write end; the child’s copy remains.

start_listener() None[source]#

Start a daemon thread that forwards data from the pipe to sys.stdout/sys.stderr.

join_listener(
timeout: float | None = None,
) None[source]#

Wait for the listener thread to finish (until EOF).