Skip to content

Commit

Permalink
Added python-3 support, implemented waiting for grpc channel readiness.
Browse files Browse the repository at this point in the history
  • Loading branch information
goord committed May 22, 2018
1 parent 2256736 commit 273c8b7
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 23 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: python
python:
- '2.7'
- '3.6'
install:
- pip install -r requirements.txt
- python setup.py install
Expand All @@ -9,6 +10,3 @@ script:
cache:
directories:
- $HOME/cache/pip



5 changes: 2 additions & 3 deletions grpc4bmi/bmi_client_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class BmiClientDocker(BmiClient):

"""
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 50001 for communication with this client. Upon
Expand All @@ -19,7 +18,6 @@ class BmiClientDocker(BmiClient):

def __init__(self, image, image_port=50051, host=None, input_dir=None, output_dir=None):
port = BmiClient.get_unique_port()
super(BmiClientDocker, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))
client = docker.from_env()
volumes = {}
self.input_dir = None
Expand All @@ -33,9 +31,10 @@ def __init__(self, image, image_port=50051, host=None, input_dir=None, output_di
self.container = client.containers.run(image, ports={str(image_port) + "/tcp": port},
volumes=volumes,
detach=True)
super(BmiClientDocker, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))

def __del__(self):
if hasattr(self,"container"):
if hasattr(self, "container"):
self.container.stop()

def initialize(self, filename):
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 @@ -17,8 +17,8 @@ def __init__(self, module_name):
port = BmiClient.get_unique_port(host)
name_options = ["--name", module_name]
port_options = ["--port", str(port)]
self.pipe = subprocess.Popen(["run-bmi-server"] + name_options + port_options, env=dict(os.environ))
time.sleep(1) # Wait until server is really running...
self.pipe = subprocess.Popen(["run-bmi-server"] + name_options + port_options, env=dict(os.environ),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
super(BmiClientSubProcess, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))

def __del__(self):
Expand Down
14 changes: 9 additions & 5 deletions grpc4bmi/bmi_grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@


class BmiClient(bmi.Bmi):

"""
Client BMI interface, implementing BMI by forwarding every function call via GRPC to the server connected to the
same port. A GRPC channel can be passed to the constructor; if not, it constructs an insecure channel on a free
port itself.
port itself. The timeout parameter indicates the model BMI startup timeout parameter (s).
"""

occupied_ports = set()

def __init__(self, channel=None):
c = BmiClient.create_grpc_channel() if channel is None else channel
self.stub = bmi_pb2_grpc.BmiServiceStub(c)
def __init__(self, channel=None, timeout=None, stub=None):
if stub is None:
c = BmiClient.create_grpc_channel() if channel is None else channel
self.stub = bmi_pb2_grpc.BmiServiceStub(c)
future = grpc.channel_ready_future(c)
future.result(timeout=timeout)
else:
self.stub = stub

def __del__(self):
del self.stub
Expand Down
4 changes: 2 additions & 2 deletions grpc4bmi/bmi_grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def getValueAtIndices(self, request, context):
indices = request.indices
index_size = request.index_size
if index_size == 2:
num_indices = len(request.indices) / index_size
num_indices = int(len(request.indices) / index_size)
indices = numpy.reshape(indices, (num_indices, index_size))
vals = self.bmi_model_.get_value_at_indices(request.name, indices)
if vals.dtype == numpy.int32:
Expand Down Expand Up @@ -139,7 +139,7 @@ def setValuePtr(self, request, context):

def setValueAtIndices(self, request, context):
index_size = request.index_size
num_indices = len(request.indices) / index_size
num_indices = int(len(request.indices) / index_size)
index_array = numpy.reshape(request.indices, newshape=(num_indices, index_size))
ints, floats, doubles = BmiServer.check_request_values(request)
if ints:
Expand Down
2 changes: 1 addition & 1 deletion grpc4bmi/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from concurrent import futures

from grpc4bmi import bmi_pb2_grpc
from bmi_grpc_server import BmiServer
from grpc4bmi.bmi_grpc_server import BmiServer

"""
Run server script, turning a BMI implementation into an executable by looping indefinitely, until interrupt signals are
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ scipy
docker
pytest
futures
git+https://github.com/csdms/bmi-python.git
git+https://github.com/csdms/bmi-tester.git
git+https://github.com/eWaterCycle/bmi-python.git
git+https://github.com/csdms/standard_names.git
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ def read(fname):
package_data[d] = files

setup(name="grpc4bmi",
version="0.1.2",
version="0.1.3",
author="Gijs van den Oord",
author_email="[email protected]",
description="Run your BMI implementation in a separate process and expose it as BMI-python with GRPC",
license="Apache License, Version 2.0",
url="https://github.com/eWaterCycle/grpc4bmi",
packages=find_packages(),
dependency_links=["https://github.com/csdms/bmi-python", "https://github.com/csdms/bmi-tester/"],
dependency_links=["https://github.com/eWaterCycle/bmi-python"],
package_data=package_data,
include_package_data=True,
long_description=open("README.md").read(),
entry_points={"console_scripts": [
"run-bmi-server = grpc4bmi.run_server:main"
]},
install_requires=["grpcio","protobuf","numpy","futures","docker"],
install_requires=["grpcio", "protobuf", "numpy", "futures", "docker"],
classifiers=["Development Status :: 3 - Alpha",
"Intended Audience :: Science/Research",
"Programming Language :: Python",
Expand Down
3 changes: 1 addition & 2 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def add_context(*args, **kwargs):


def make_bmi_classes(init=False):
client = BmiClient()
client.stub = ServerWrapper(BmiServer("BmiHeat", "heat"))
client = BmiClient(stub=ServerWrapper(BmiServer("BmiHeat", "heat")))
local = BmiHeat()
if init:
numpy.random.seed(0)
Expand Down

0 comments on commit 273c8b7

Please sign in to comment.