Stop Ray processes manually on the local machine.
(force: bool, grace_period: int)
| 1339 | @add_click_logging_options |
| 1340 | @PublicAPI |
| 1341 | def stop(force: bool, grace_period: int): |
| 1342 | """Stop Ray processes manually on the local machine.""" |
| 1343 | is_linux = sys.platform.startswith("linux") |
| 1344 | total_procs_found = 0 |
| 1345 | total_procs_stopped = 0 |
| 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? |
no test coverage detected
searching dependent graphs…