MCPcopy Index your code
hub / github.com/python/cpython / _handle_live_run

Function _handle_live_run

Lib/profiling/sampling/cli.py:1129–1196  ·  view source on GitHub ↗

Handle live mode for running a script/module.

(args)

Source from the content-addressed store, hash-verified

1127
1128
1129def _handle_live_run(args):
1130 """Handle live mode for running a script/module."""
1131 # Build the command to run
1132 if args.module:
1133 cmd = (sys.executable, "-m", args.target, *args.args)
1134 else:
1135 cmd = (sys.executable, args.target, *args.args)
1136
1137 # Run with synchronization, suppressing output for live mode
1138 try:
1139 process = _run_with_sync(cmd, suppress_output=True)
1140 except RuntimeError as e:
1141 sys.exit(f"Error: {e}")
1142
1143 mode = _parse_mode(args.mode)
1144
1145 # Determine skip_idle based on mode
1146 skip_idle = mode != PROFILING_MODE_WALL
1147
1148 # Create live collector with default settings
1149 collector = LiveStatsCollector(
1150 args.sample_interval_usec,
1151 skip_idle=skip_idle,
1152 sort_by="tottime", # Default initial sort
1153 limit=20, # Default limit
1154 pid=process.pid,
1155 mode=mode,
1156 opcodes=args.opcodes,
1157 async_aware=args.async_mode if args.async_aware else None,
1158 )
1159
1160 # Profile the subprocess in live mode
1161 try:
1162 sample_live(
1163 process.pid,
1164 collector,
1165 duration_sec=args.duration,
1166 all_threads=args.all_threads,
1167 realtime_stats=args.realtime_stats,
1168 mode=mode,
1169 async_aware=args.async_mode if args.async_aware else None,
1170 native=args.native,
1171 gc=args.gc,
1172 opcodes=args.opcodes,
1173 blocking=args.blocking,
1174 )
1175 finally:
1176 # Clean up the subprocess and get any error output
1177 returncode = process.poll()
1178 if returncode is None:
1179 # Process still running - terminate it
1180 process.terminate()
1181 try:
1182 process.wait(timeout=_PROCESS_KILL_TIMEOUT_SEC)
1183 except subprocess.TimeoutExpired:
1184 process.kill()
1185 # Ensure process is fully terminated
1186 process.wait()

Callers 1

_handle_runFunction · 0.85

Calls 11

sample_liveFunction · 0.90
_run_with_syncFunction · 0.85
_parse_modeFunction · 0.85
LiveStatsCollectorClass · 0.85
exitMethod · 0.45
pollMethod · 0.45
terminateMethod · 0.45
waitMethod · 0.45
killMethod · 0.45
readMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…