Skip to content

Commit

Permalink
Added timeout argument to Docker, Singularity and Subprocess clients.
Browse files Browse the repository at this point in the history
Refs #63
  • Loading branch information
sverhoeven committed Mar 13, 2020
1 parent 659304b commit 5523d24
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions grpc4bmi/bmi_client_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ def __init__(self, message, exitcode, logs, *args):

class BmiClientDocker(BmiClient):
"""
BMI GRPC client for dockerized server processes: the initialization launches the docker container which should have the
BMI gRPC client for dockerized server processes: the initialization launches the docker container which should have the
run-bmi-server as its command. Also, it should expose the tcp port 55555 for communication with this client. Upon
destruction, this class terminates the corresponding docker server.
Args:
image (str): Docker image name of grpc4bmi wrapped model
image_port (int): Port of server inside the image
Expand All @@ -45,7 +44,9 @@ class BmiClientDocker(BmiClient):
user (str): Username or UID of Docker container
remove (bool): Automatically remove the container and logs when it exits.
delay (int): Seconds to wait for Docker container to startup, before connecting to it
timeout (int): Seconds to wait for gRPC client to connect to server
extra_volumes (Dict[str,Dict]): Extra volumes to attach to Docker container.
The key is either the hosts path or a volume name and the value is a dictionary with the keys:
- ``bind`` The path to mount the volume inside the container
Expand All @@ -65,7 +66,7 @@ class BmiClientDocker(BmiClient):
def __init__(self, image, image_port=55555, host=None,
input_dir=None, output_dir=None,
user=os.getuid(), remove=False,
delay=5, extra_volumes=None):
delay=5, timeout=None, extra_volumes=None):
port = BmiClient.get_unique_port()
client = docker.from_env()
volumes = {}
Expand Down Expand Up @@ -104,7 +105,7 @@ def __init__(self, image, image_port=55555, host=None,
msg = f'Failed to start Docker container with image {image}, Container log: {logs}'
raise DeadDockerContainerException(msg, exitcode, logs)

super(BmiClientDocker, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))
super(BmiClientDocker, self).__init__(BmiClient.create_grpc_channel(port=port, host=host), timeout=timeout)

def __del__(self):
if hasattr(self, "container"):
Expand Down
5 changes: 3 additions & 2 deletions grpc4bmi/bmi_client_singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ class BmiClientSingularity(BmiClient):
image: Singularity image. For Docker Hub image use `docker://*`.
input_dir (str): Directory for input files of model
output_dir (str): Directory for input files of model
timeout (int): Seconds to wait for gRPC client to connect to server
"""
INPUT_MOUNT_POINT = "/data/input"
OUTPUT_MOUNT_POINT = "/data/output"

def __init__(self, image, input_dir=None, output_dir=None):
def __init__(self, image, input_dir=None, output_dir=None, timeout=None):
check_singularity_version()
host = 'localhost'
port = BmiClient.get_unique_port(host)
Expand All @@ -70,7 +71,7 @@ def __init__(self, image, input_dir=None, output_dir=None):
env['BMI_PORT'] = str(port)
logging.info(f'Running {image} singularity container on port {port}')
self.container = subprocess.Popen(args, env=env, preexec_fn=os.setsid)
super(BmiClientSingularity, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))
super(BmiClientSingularity, self).__init__(BmiClient.create_grpc_channel(port=port, host=host), timeout=timeout)

def __del__(self):
if hasattr(self, "container"):
Expand Down
4 changes: 2 additions & 2 deletions grpc4bmi/bmi_client_subproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class BmiClientSubProcess(BmiClient):
>>> mymodel = BmiClientSubProcess(<PACKAGE>.<MODULE>.<CLASS>)
"""

def __init__(self, module_name, path=None):
def __init__(self, module_name, path=None, timeout=None):
host = "localhost"
port = BmiClient.get_unique_port(host)
name_options = ["--name", module_name]
port_options = ["--port", str(port)]
path_options = ["--path", path] if path else []
self.pipe = subprocess.Popen(["run-bmi-server"] + name_options + port_options + path_options, env=dict(os.environ))
time.sleep(1)
super(BmiClientSubProcess, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))
super(BmiClientSubProcess, self).__init__(BmiClient.create_grpc_channel(port=port, host=host), timeout=timeout)

def __del__(self):
self.pipe.terminate()
Expand Down

0 comments on commit 5523d24

Please sign in to comment.