Skip to content

Commit 6c5bc60

Browse files
Address comments
1 parent 7f32851 commit 6c5bc60

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

Doc/library/profiling.sampling.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,20 @@ Output options
14911491
named ``<format>_<PID>.<ext>`` (for example, ``flamegraph_12345.html``).
14921492
:option:`--heatmap` creates a directory named ``heatmap_<PID>``.
14931493

1494+
For HTML outputs (:option:`--flamegraph` and :option:`--heatmap`), the
1495+
generated file or directory is automatically opened in your default web
1496+
browser after profiling completes. Use :option:`--no-browser` to disable
1497+
this behavior. When using :option:`--subprocesses`, only the main process
1498+
output is opened automatically to avoid opening multiple browser tabs.
1499+
1500+
.. option:: --no-browser
1501+
1502+
Disable automatic browser opening for HTML output (:option:`--flamegraph`
1503+
and :option:`--heatmap`). By default, HTML visualizations are opened in
1504+
your default web browser after generation. When profiling with
1505+
:option:`--subprocesses`, only the main process opens the browser by
1506+
default; subprocess outputs are never auto-opened.
1507+
14941508

14951509
pstats display options
14961510
----------------------

Lib/profiling/sampling/cli.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ def _build_child_profiler_args(args):
148148
if args.format != "pstats":
149149
child_args.append(f"--{args.format}")
150150

151+
# Always add --no-browser for child profilers to avoid opening multiple browser tabs
152+
child_args.append("--no-browser")
153+
151154
return child_args
152155

153156

@@ -496,7 +499,8 @@ def _add_format_options(parser, include_compression=True, include_binary=True):
496499
output_group.add_argument(
497500
"--no-browser",
498501
action="store_true",
499-
help="Disable automatic browser opening for HTML output (flamegraph, heatmap)",
502+
help="Disable automatic browser opening for HTML output (flamegraph, heatmap). "
503+
"When using --subprocesses, only the main process opens the browser by default",
500504
)
501505

502506

@@ -613,14 +617,14 @@ def _open_in_browser(path):
613617
if os.path.exists(index_path):
614618
abs_path = index_path
615619
else:
616-
print(f"Warning: Could not find index.html in {path}")
620+
print(f"Warning: Could not find index.html in {path}", file=sys.stderr)
617621
return
618622

619623
file_url = f"file://{abs_path}"
620624
try:
621625
webbrowser.open(file_url)
622626
except Exception as e:
623-
print(f"Warning: Could not open browser: {e}")
627+
print(f"Warning: Could not open browser: {e}", file=sys.stderr)
624628

625629

626630
def _handle_output(collector, args, pid, mode):

Lib/test/test_profiling/test_sampling_profiler/test_children.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ def assert_flag_value_pair(flag, value):
438438
child_args,
439439
f"Flag '--flamegraph' not found in args: {child_args}",
440440
)
441+
self.assertIn(
442+
"--no-browser",
443+
child_args,
444+
f"Flag '--no-browser' not found in args: {child_args}",
445+
)
441446

442447
def test_build_child_profiler_args_no_gc(self):
443448
"""Test building CLI args with --no-gc."""
@@ -992,6 +997,7 @@ def test_subprocesses_flag_with_flamegraph_output(self):
992997
"-r",
993998
"100",
994999
"--flamegraph",
1000+
"--no-browser",
9951001
"-o",
9961002
output_file,
9971003
script_file,

0 commit comments

Comments
 (0)