From f9f41e5a1a6f054cfe19898786b0494a5c750ee7 Mon Sep 17 00:00:00 2001 From: Edward Oakes Date: Wed, 22 Apr 2020 11:51:27 -0500 Subject: [PATCH] [serve] Fix nonblocking serve.init() (#8068) --- python/ray/serve/BUILD | 12 +++++++++-- python/ray/serve/http_proxy.py | 9 +++++---- python/ray/serve/tests/test_nonblocking.py | 23 ++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 python/ray/serve/tests/test_nonblocking.py diff --git a/python/ray/serve/BUILD b/python/ray/serve/BUILD index fbe61852d..348a01394 100644 --- a/python/ray/serve/BUILD +++ b/python/ray/serve/BUILD @@ -13,7 +13,7 @@ py_library( py_test( name = "test_serve", size = "medium", - srcs = glob(["tests/*.py"]), + srcs = glob(["tests/*.py"], exclude=["tests/test_nonblocking.py"]), tags = ["exclusive"], deps = [":serve_lib"], ) @@ -26,6 +26,14 @@ py_test( deps = [":serve_lib"] ) +py_test( + name = "test_nonblocking", + size = "small", + srcs = glob(["tests/test_nonblocking.py"]), + tags = ["exclusive"], + deps = [":serve_lib"], +) + # Make sure the example showing in doc is tested py_test( name = "quickstart_class", @@ -41,4 +49,4 @@ py_test( srcs = glob(["examples/doc/*.py"]), tags = ["exclusive"], deps = [":serve_lib"] -) \ No newline at end of file +) diff --git a/python/ray/serve/http_proxy.py b/python/ray/serve/http_proxy.py index d8a54724c..a7d9a2a33 100644 --- a/python/ray/serve/http_proxy.py +++ b/python/ray/serve/http_proxy.py @@ -26,11 +26,11 @@ class HTTPProxy: # blocks forever """ - def __init__(self): + async def fetch_config_from_master(self): assert ray.is_initialized() master = ray.util.get_actor(SERVE_MASTER_NAME) - self.route_table, [self.router_handle] = ray.get( - master.get_http_proxy_config.remote()) + self.route_table, [self.router_handle + ] = await master.get_http_proxy_config.remote() def set_route_table(self, route_table): self.route_table = route_table @@ -163,8 +163,9 @@ class HTTPProxy: @ray.remote class HTTPProxyActor: - def __init__(self, host, port): + async def __init__(self, host, port): self.app = HTTPProxy() + await self.app.fetch_config_from_master() self.host = host self.port = port diff --git a/python/ray/serve/tests/test_nonblocking.py b/python/ray/serve/tests/test_nonblocking.py new file mode 100644 index 000000000..11b0f2b4f --- /dev/null +++ b/python/ray/serve/tests/test_nonblocking.py @@ -0,0 +1,23 @@ +import requests +import sys + +from ray import serve + + +def test_nonblocking(): + serve.init() + serve.create_endpoint("endpoint", "/api") + + def function(flask_request): + return {"method": flask_request.method} + + serve.create_backend(function, "echo:v1") + serve.set_traffic("endpoint", {"echo:v1": 1.0}) + + resp = requests.get("http://127.0.0.1:8000/api").json()["method"] + assert resp == "GET" + + +if __name__ == "__main__": + import pytest + sys.exit(pytest.main(["-v", __file__]))