[V1] Simplify Shutdown (#11659)
This commit is contained in:
+22
-20
@@ -1,3 +1,4 @@
|
||||
import multiprocessing
|
||||
import os
|
||||
import weakref
|
||||
from collections.abc import Sequence
|
||||
@@ -91,8 +92,6 @@ class BackgroundProcHandle:
|
||||
target_fn: Callable,
|
||||
process_kwargs: Dict[Any, Any],
|
||||
):
|
||||
self._finalizer = weakref.finalize(self, self.shutdown)
|
||||
|
||||
context = get_mp_context()
|
||||
reader, writer = context.Pipe(duplex=False)
|
||||
|
||||
@@ -102,11 +101,11 @@ class BackgroundProcHandle:
|
||||
process_kwargs["ready_pipe"] = writer
|
||||
process_kwargs["input_path"] = input_path
|
||||
process_kwargs["output_path"] = output_path
|
||||
self.input_path = input_path
|
||||
self.output_path = output_path
|
||||
|
||||
# Run Detokenizer busy loop in background process.
|
||||
# Run busy loop in background process.
|
||||
self.proc = context.Process(target=target_fn, kwargs=process_kwargs)
|
||||
self._finalizer = weakref.finalize(self, shutdown, self.proc,
|
||||
input_path, output_path)
|
||||
self.proc.start()
|
||||
|
||||
# Wait for startup.
|
||||
@@ -114,21 +113,24 @@ class BackgroundProcHandle:
|
||||
raise RuntimeError(f"{process_name} initialization failed. "
|
||||
"See root cause above.")
|
||||
|
||||
def __del__(self):
|
||||
self.shutdown()
|
||||
|
||||
def shutdown(self):
|
||||
# Shutdown the process if needed.
|
||||
if hasattr(self, "proc") and self.proc.is_alive():
|
||||
self.proc.terminate()
|
||||
self.proc.join(5)
|
||||
self._finalizer()
|
||||
|
||||
if self.proc.is_alive():
|
||||
kill_process_tree(self.proc.pid)
|
||||
|
||||
# Remove zmq ipc socket files
|
||||
ipc_sockets = [self.output_path, self.input_path]
|
||||
for ipc_socket in ipc_sockets:
|
||||
socket_file = ipc_socket.replace("ipc://", "")
|
||||
if os and os.path.exists(socket_file):
|
||||
os.remove(socket_file)
|
||||
# Note(rob): shutdown function cannot be a bound method,
|
||||
# else the gc cannot collect the object.
|
||||
def shutdown(proc: multiprocessing.Process, input_path: str, output_path: str):
|
||||
# Shutdown the process.
|
||||
if proc.is_alive():
|
||||
proc.terminate()
|
||||
proc.join(5)
|
||||
|
||||
if proc.is_alive():
|
||||
kill_process_tree(proc.pid)
|
||||
|
||||
# Remove zmq ipc socket files.
|
||||
ipc_sockets = [output_path, input_path]
|
||||
for ipc_socket in ipc_sockets:
|
||||
socket_file = ipc_socket.replace("ipc://", "")
|
||||
if os and os.path.exists(socket_file):
|
||||
os.remove(socket_file)
|
||||
|
||||
Reference in New Issue
Block a user