Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the ros2cli output always line buffered #659

Merged
merged 4 commits into from
Aug 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ros2cli/ros2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# limitations under the License.

import argparse
import builtins
import functools
import signal
import sys

from ros2cli.command import add_subparsers_on_demand

Expand All @@ -29,6 +32,14 @@ def main(*, script_name='ros2', argv=None, description=None, extension=None):
description=description,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
'--use-python-default-buffering',
action='store_true',
default=False,
help=(
'Do not force line buffering in stdout and instead use the python default buffering, '
'which might be affected by PYTHONUNBUFFERED/-u and depends on whatever stdout is '
'interactive or not'))

# add arguments for command extension(s)
if extension:
Expand All @@ -53,6 +64,17 @@ def main(*, script_name='ros2', argv=None, description=None, extension=None):
# parse the command line arguments
args = parser.parse_args(args=argv)

if not args.use_python_default_buffering:
# Make the output always line buffered.
# TextIoWrapper has a reconfigure() method, call that if available.
# https://docs.python.org/3/library/io.html#io.TextIOWrapper.reconfigure
try:
sys.stdout.reconfigure(line_buffering=True)
except AttributeError:
# if stdout is not a TextIoWrapper instance, or we're using python older than 3.7,
# force line buffering by patching print
builtins.print = functools.partial(print, flush=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivanpauno FYI if the decorator monitors the file keyword argument of print, you can limit its action to sys.stdout.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I didn't feel that was really needed though.
do you think we need a patch for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it's unlikely anyone will ever hit it before Python 3.6 support is gone.


if extension is None:
# get extension identified by the passed command (if available)
extension = getattr(args, selected_extension_key, None)
Expand Down