Skip to content

Commit fde2a37

Browse files
committed
use pkill instead of psutil
1 parent 953ce56 commit fde2a37

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

metaflow/subprocess_manager.py

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import os
22
import sys
3+
import time
34
import signal
4-
import psutil
55
import shutil
66
import asyncio
77
import tempfile
8+
import subprocess
89
from typing import List, Dict, Optional, Callable
910

1011

1112
def kill_process_and_descendants(pid, termination_timeout):
12-
def on_terminate(proc):
13-
print("process %s terminated" % proc)
13+
try:
14+
subprocess.check_call(["pkill", "-TERM", "-P", str(pid)])
15+
except subprocess.CalledProcessError as e:
16+
pass
17+
18+
time.sleep(termination_timeout)
1419

1520
try:
16-
parent = psutil.Process(pid)
17-
children = parent.children(recursive=True)
18-
for p in children:
19-
p.terminate()
20-
_, alive = psutil.wait_procs(
21-
children, timeout=termination_timeout, callback=on_terminate
22-
)
23-
for p in alive:
24-
p.kill()
25-
except psutil.NoSuchProcess:
21+
subprocess.check_call(["pkill", "-KILL", "-P", str(pid)])
22+
except subprocess.CalledProcessError as e:
2623
pass
2724

2825

@@ -226,16 +223,11 @@ def cleanup(self):
226223
if self.run_called:
227224
shutil.rmtree(self.temp_dir, ignore_errors=True)
228225

229-
async def kill(self, termination_timeout: float = 5):
226+
async def kill(self, termination_timeout: float = 1):
230227
"""Kill the subprocess and its descendants."""
231228

232229
if self.process is not None:
233230
kill_process_and_descendants(self.process.pid, termination_timeout)
234-
self.process.terminate()
235-
try:
236-
await asyncio.wait_for(self.process.wait(), termination_timeout)
237-
except asyncio.TimeoutError:
238-
self.process.kill()
239231
else:
240232
print("No process to kill.")
241233

0 commit comments

Comments
 (0)