Kill the training script if the viewer ports are conflicting with other processes (#352)

kills program if conflicting ports for the viewer
This commit is contained in:
Ethan Weber authored and GitHub committed 2022-09-07 12:32:31 -07:00
1 parent 512a51f714
commit 7aa6ca9948
4 files changed
+41 -4

No files matched your search

+3
View File
@@ -334,6 +334,7 @@ class PipelineConfig(InstantiateConfig):
class ViewerConfig(PrintableConfig): class ViewerConfig(PrintableConfig):
"""Configuration for viewer instantiation""" """Configuration for viewer instantiation"""
log_filename: Optional[Path] = None
enable: bool = False enable: bool = False
zmq_url: str = "tcp://127.0.0.1:6000" zmq_url: str = "tcp://127.0.0.1:6000"
launch_bridge_server: bool = True launch_bridge_server: bool = True
@@ -403,3 +404,5 @@ class Config(PrintableConfig):
self.trainer.model_dir = self.base_dir / self.trainer.relative_model_dir self.trainer.model_dir = self.base_dir / self.trainer.relative_model_dir
for curr_writer in self.logging.writer: for curr_writer in self.logging.writer:
curr_writer.log_dir = self.base_dir / curr_writer.relative_log_dir curr_writer.log_dir = self.base_dir / curr_writer.relative_log_dir
if self.viewer.log_filename is None:
self.viewer.log_filename = self.base_dir / "viewer_log_filename.txt"
+31 -2
View File
@@ -18,14 +18,21 @@ Code to use the viewer as a subprocess.
import atexit import atexit
import os import os
import signal
import subprocess import subprocess
import sys import sys
from typing import Optional import threading
import time
from typing import Union
from rich.console import Console
from nerfactory.viewer.server import server from nerfactory.viewer.server import server
CONSOLE = Console()
def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, log_filename: Optional[str] = None):
def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, log_filename: Union[str, None]):
"""Runs the viewer bridge server as a subprocess. """Runs the viewer bridge server as a subprocess.
Args: Args:
@@ -53,4 +60,26 @@ def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, l
process.kill() process.kill()
process.wait() process.wait()
def poll_process():
"""
Continually check to see if the viewer bridge server process is still running and has not failed.
If it fails, alert the user and exit the entire program.
"""
while process.poll() is None:
time.sleep(0.5)
string = f"\nThe viewer bridge server subprocess failed. Please check the log file {log_filename}.\n"
string += (
"You likely have to modify --viewer.zmq-url and/or --viewer.websocket-port in the "
"config to avoid conflicting ports.\n"
)
string += "Try modifying --viewer.zmq-url tcp://127.0.0.1:6000 --viewer.websocket-port 7007\n"
CONSOLE.print(f"[bold red]{string}")
cleanup(process)
# This exists the entire program. sys.exit() will only kill the thread that this runs in.
os.kill(os.getpid(), signal.SIGKILL)
# continually check to see if the process stopped
t1 = threading.Thread(target=poll_process)
t1.daemon = True
t1.start()
atexit.register(cleanup, process) atexit.register(cleanup, process)
+6 -1
View File
@@ -195,7 +195,12 @@ class VisualizerState:
# start the viewer bridge server # start the viewer bridge server
zmq_port = int(self.config.zmq_url.split(":")[-1]) zmq_port = int(self.config.zmq_url.split(":")[-1])
websocket_port = self.config.websocket_port websocket_port = self.config.websocket_port
run_viewer_bridge_server_as_subprocess(zmq_port, websocket_port) self.config.log_filename.parent.mkdir(exist_ok=True)
run_viewer_bridge_server_as_subprocess(
zmq_port, websocket_port, log_filename=str(self.config.log_filename)
)
# TODO(ethan): move this into the writer such that it's at the bottom
# of the logging stack and easy to see and click
# TODO(ethan): log the output of the viewer bridge server in a file where the training logs go # TODO(ethan): log the output of the viewer bridge server in a file where the training logs go
print("\n") print("\n")
self.viewer_url = ( self.viewer_url = (
+1 -1
View File
@@ -12,7 +12,7 @@ def test_run_subprocess():
zmq_port = 6000 + 10 # don't use the default ports to avoid conflicting with current training jobs zmq_port = 6000 + 10 # don't use the default ports to avoid conflicting with current training jobs
websocket_port = 7007 + 10 websocket_port = 7007 + 10
print("Starting the viewer bridge server as a subprocess.") print("Starting the viewer bridge server as a subprocess.")
run_viewer_bridge_server_as_subprocess(zmq_port, websocket_port) run_viewer_bridge_server_as_subprocess(zmq_port, websocket_port, log_filename=None)
print("Started the viewer bridge server as a subprocess.") print("Started the viewer bridge server as a subprocess.")
# do something will the viewer bridge server runs # do something will the viewer bridge server runs
time.sleep(1) time.sleep(1)