Skip to content

Commit fdcfc99

Browse files
authored
RSDK-7014: Configure python to flush to stdout despite not being connected to a tty. (#568)
1 parent 3f78f5d commit fdcfc99

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/viam/module/module.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import sys
23
from inspect import iscoroutinefunction
34
from threading import Lock
@@ -64,6 +65,15 @@ def from_args(cls) -> Self:
6465
return cls(address, log_level=log_level)
6566

6667
def __init__(self, address: str, *, log_level: int = logging.INFO) -> None:
68+
# When a module is launched by viam-server, its stdout is not connected to a tty. In
69+
# response, python disables line buffering, which prevents `print` statements from being
70+
# immediately flushed to viam-server. This behavior can be confusing, interfere with
71+
# debugging, and is non-standard when compared to other languages. Here, stdout and stderr
72+
# are reconfigured to immediately flush.
73+
if isinstance(sys.stdout, io.TextIOWrapper):
74+
sys.stdout.reconfigure(line_buffering=True)
75+
if isinstance(sys.stderr, io.TextIOWrapper):
76+
sys.stderr.reconfigure(line_buffering=True)
6777
self._address = address
6878
self.server = Server(resources=[], module_service=ModuleRPCService(self))
6979
self._log_level = log_level

0 commit comments

Comments
 (0)