MCPcopy
hub / github.com/ray-project/ray / kill_procs

Function kill_procs

python/ray/scripts/scripts.py:1348–1451  ·  view source on GitHub ↗

Find all processes from `processes_to_kill` and terminate them. Unless `force` is specified, it gracefully kills processes. If processes are not cleaned within `grace_period`, it force kill all remaining processes. Returns: total_procs_found: Total numbe

(
        force: bool, grace_period: int, processes_to_kill: List[str]
    )

Source from the content-addressed store, hash-verified

1346 procs_not_gracefully_killed = []
1347
1348 def kill_procs(
1349 force: bool, grace_period: int, processes_to_kill: List[str]
1350 ) -> Tuple[int, int, List[psutil.Process]]:
1351 """Find all processes from `processes_to_kill` and terminate them.
1352
1353 Unless `force` is specified, it gracefully kills processes. If
1354 processes are not cleaned within `grace_period`, it force kill all
1355 remaining processes.
1356
1357 Returns:
1358 total_procs_found: Total number of processes found from
1359 `processes_to_kill` is added.
1360 total_procs_stopped: Total number of processes gracefully
1361 stopped from `processes_to_kill` is added.
1362 procs_not_gracefully_killed: If processes are not killed
1363 gracefully, they are added here.
1364 """
1365 process_infos = []
1366 for proc in psutil.process_iter(["name", "cmdline"]):
1367 try:
1368 process_infos.append((proc, proc.name(), proc.cmdline()))
1369 except psutil.Error:
1370 pass
1371
1372 stopped = []
1373 for keyword, filter_by_cmd in processes_to_kill:
1374 if filter_by_cmd and is_linux and len(keyword) > 15:
1375 # getting here is an internal bug, so we do not use cli_logger
1376 msg = (
1377 "The filter string should not be more than {} "
1378 "characters. Actual length: {}. Filter: {}"
1379 ).format(15, len(keyword), keyword)
1380 raise ValueError(msg)
1381
1382 found = []
1383 for candidate in process_infos:
1384 proc, proc_cmd, proc_args = candidate
1385 corpus = (
1386 proc_cmd if filter_by_cmd else subprocess.list2cmdline(proc_args)
1387 )
1388 if keyword in corpus:
1389 found.append(candidate)
1390 for proc, proc_cmd, proc_args in found:
1391 proc_string = str(subprocess.list2cmdline(proc_args))
1392 try:
1393 if force:
1394 proc.kill()
1395 else:
1396 # TODO(mehrdadn): On Windows, this is forceful termination.
1397 # We don't want CTRL_BREAK_EVENT, because that would
1398 # terminate the entire process group. What to do?
1399 proc.terminate()
1400
1401 if force:
1402 cli_logger.verbose(
1403 "Killed `{}` {} ",
1404 cf.bold(proc_string),
1405 cf.dimmed("(via SIGKILL)"),

Callers 1

stopFunction · 0.85

Calls 7

verboseMethod · 0.80
killMethod · 0.65
appendMethod · 0.45
nameMethod · 0.45
formatMethod · 0.45
terminateMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…