unaiverse.utils.os_fd_capture¶
The native networking library of the node writes its log lines straight to the file descriptors 1 and 2 of the process. Replacing sys.stdout in Python does not catch them because the library never goes through sys.stdout. FdCapture redirects the descriptor itself into a pipe and hands every line that arrives to a callback. The networking layer uses two instances (one for standard output and one for standard error) to send those lines to the P2P channel of the logger when NODE_LIBP2PLOG=1. A developer rarely needs it directly. It also works for any C extension that prints on the terminal.
import os
from unaiverse.utils.os_fd_capture import FdCapture
lines = []
with FdCapture(1, lines.append):
os.system("echo from a child process") # Written to fd 1 by another program
print(lines) # ['from a child process']
FdCapture
¶
FdCapture(fd: int, callback: Callable[[str], None], encoding: str = 'utf-8', errors: str = 'replace')
Source code in unaiverse/utils/os_fd_capture.py
Captures one of the two standard file descriptors.
| Argument | Type | Default | Meaning |
|---|---|---|---|
fd |
int |
required | 1 (standard output) or 2 (standard error). Any other value raises ValueError. |
callback |
Callable[[str], None] |
required | Called with every non-empty line stripped of its trailing newline and carriage return. |
encoding |
str |
"utf-8" |
The encoding of the captured bytes. |
errors |
str |
"replace" |
The error handler of the decoding. |
While the capture is active, Python output takes a different path from native output. start() points sys.stdout (or sys.stderr) to a copy of the original descriptor. The print calls of Python (the screen output of the logger included) therefore keep reaching the terminal and do not enter the pipe. Only the writes to the descriptor number itself reach the callback.
The callback runs in a daemon thread named FdCapture-fd1 or FdCapture-fd2. An exception raised by the callback is ignored. A partial line still in the buffer when the pipe closes reaches the callback too. The class is a context manager and with calls start() and stop().
The copy of the original descriptor while the capture is active. It is None when the capture is not active. Writing to it reaches the terminal without entering the pipe.
start
¶
Source code in unaiverse/utils/os_fd_capture.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
Starts the capture. It flushes the Python stream, duplicates the descriptor, creates a pipe, moves the write end over the descriptor with dup2, points sys.stdout or sys.stderr to the duplicate and starts the reader thread. A call on an active capture does nothing. When a step fails the method undoes the previous ones and raises the exception again.
stop
¶
Source code in unaiverse/utils/os_fd_capture.py
Stops the capture. It restores sys.stdout or sys.stderr and the original descriptor, closes the write end of the pipe and waits up to timeout seconds (default 2.0) for the reader thread to deliver the remaining lines. A call on an inactive capture does nothing.