diff --git a/python/ray/serve/examples/echo.py b/python/ray/serve/examples/echo.py index f722b63dd..eb2f37358 100644 --- a/python/ray/serve/examples/echo.py +++ b/python/ray/serve/examples/echo.py @@ -2,12 +2,24 @@ Example service that prints out http context. """ +import json import time +from pygments import formatters, highlight, lexers + import requests from ray import serve -from ray.serve.utils import pformat_color_json + + +def pformat_color_json(d): + """Use pygments to pretty format and colorize dictionary""" + formatted_json = json.dumps(d, sort_keys=True, indent=4) + + colorful_json = highlight(formatted_json, lexers.JsonLexer(), + formatters.TerminalFormatter()) + + return colorful_json def echo(flask_request): diff --git a/python/ray/serve/examples/echo_actor.py b/python/ray/serve/examples/echo_actor.py index a919d962e..fe6f4aa98 100644 --- a/python/ray/serve/examples/echo_actor.py +++ b/python/ray/serve/examples/echo_actor.py @@ -5,13 +5,25 @@ come from either web (parsing Flask request) or python call. This actor can be called from HTTP as well as from Python. """ +import json import time +from pygments import formatters, highlight, lexers + import requests import ray from ray import serve -from ray.serve.utils import pformat_color_json + + +def pformat_color_json(d): + """Use pygments to pretty format and colorize dictionary""" + formatted_json = json.dumps(d, sort_keys=True, indent=4) + + colorful_json = highlight(formatted_json, lexers.JsonLexer(), + formatters.TerminalFormatter()) + + return colorful_json class MagicCounter: diff --git a/python/ray/serve/examples/echo_actor_batch.py b/python/ray/serve/examples/echo_actor_batch.py index 5173a7c84..9ad8dd55e 100644 --- a/python/ray/serve/examples/echo_actor_batch.py +++ b/python/ray/serve/examples/echo_actor_batch.py @@ -5,13 +5,25 @@ The queries incoming to this actor are batched. This actor can be called from HTTP as well as from Python. """ +import json import time +from pygments import formatters, highlight, lexers + import requests import ray from ray import serve -from ray.serve.utils import pformat_color_json + + +def pformat_color_json(d): + """Use pygments to pretty format and colorize dictionary""" + formatted_json = json.dumps(d, sort_keys=True, indent=4) + + colorful_json = highlight(formatted_json, lexers.JsonLexer(), + formatters.TerminalFormatter()) + + return colorful_json class MagicCounter: diff --git a/python/ray/serve/examples/echo_error.py b/python/ray/serve/examples/echo_error.py index 670a0fb99..bbd1302b7 100644 --- a/python/ray/serve/examples/echo_error.py +++ b/python/ray/serve/examples/echo_error.py @@ -13,13 +13,25 @@ This shows that error is hidden from HTTP side but always visible when calling from Python. """ +import json import time +from pygments import formatters, highlight, lexers + import requests import ray from ray import serve -from ray.serve.utils import pformat_color_json + + +def pformat_color_json(d): + """Use pygments to pretty format and colorize dictionary""" + formatted_json = json.dumps(d, sort_keys=True, indent=4) + + colorful_json = highlight(formatted_json, lexers.JsonLexer(), + formatters.TerminalFormatter()) + + return colorful_json def echo(_): diff --git a/python/ray/serve/examples/echo_fixed_packing.py b/python/ray/serve/examples/echo_fixed_packing.py deleted file mode 100644 index 7c294c8c7..000000000 --- a/python/ray/serve/examples/echo_fixed_packing.py +++ /dev/null @@ -1,47 +0,0 @@ -""" -Example showing fixed packing policy. The outputs from -v1 and v2 will be coming according to packing_num specified! -This is a packed round robin example. First batch of packing_num -(five in this example) queries would go to 'echo:v1' backend and -then next batch of packing_num queries would go to 'echo:v2' -backend. -""" -import time - -import requests - -from ray import serve -from ray.serve.utils import pformat_color_json - - -def echo_v1(_): - return "v1" - - -def echo_v2(_): - return "v2" - - -# specify the router policy as FixedPacking with packing num as 5 -serve.init( - queueing_policy=serve.RoutePolicy.FixedPacking, - policy_kwargs={"packing_num": 5}) - -# create first backend -serve.create_backend("echo:v1", echo_v1) - -# create service backed by the first backend -serve.create_endpoint("my_endpoint", backend="echo:v1", route="/echo") - -# create second backend -serve.create_backend("echo:v2", echo_v2) - -# split the service between the two backends -serve.set_traffic("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5}) - -while True: - resp = requests.get("http://127.0.0.1:8000/echo").json() - print(pformat_color_json(resp)) - - print("...Sleeping for 2 seconds...") - time.sleep(2) diff --git a/python/ray/serve/examples/echo_full.py b/python/ray/serve/examples/echo_full.py index 84d6cf60d..0caf11d9b 100644 --- a/python/ray/serve/examples/echo_full.py +++ b/python/ray/serve/examples/echo_full.py @@ -2,13 +2,26 @@ Full example of ray.serve module """ +import json import time +from pygments import formatters, highlight, lexers + import requests import ray import ray.serve as serve -from ray.serve.utils import pformat_color_json + + +def pformat_color_json(d): + """Use pygments to pretty format and colorize dictionary""" + formatted_json = json.dumps(d, sort_keys=True, indent=4) + + colorful_json = highlight(formatted_json, lexers.JsonLexer(), + formatters.TerminalFormatter()) + + return colorful_json + # initialize ray serve system. serve.init() diff --git a/python/ray/serve/examples/echo_round_robin.py b/python/ray/serve/examples/echo_round_robin.py deleted file mode 100644 index 4dc376ae4..000000000 --- a/python/ray/serve/examples/echo_round_robin.py +++ /dev/null @@ -1,41 +0,0 @@ -""" -Example showing round robin policy. The outputs from -v1 and v2 will be (almost) interleaved as queries get processed. -""" -import time - -import requests - -from ray import serve -from ray.serve.utils import pformat_color_json - - -def echo_v1(_): - return "v1" - - -def echo_v2(_): - return "v2" - - -# specify the router policy as RoundRobin -serve.init(queueing_policy=serve.RoutePolicy.RoundRobin) - -# create first backend -serve.create_backend("echo:v1", echo_v1) - -# create a service backend by the first backend -serve.create_endpoint("my_endpoint", backend="echo:v1", route="/echo") - -# create second backend -serve.create_backend("echo:v2", echo_v2) - -# split the service between the two backends -serve.set_traffic("my_endpoint", {"echo:v1": 0.5, "echo:v2": 0.5}) - -while True: - resp = requests.get("http://127.0.0.1:8000/echo").json() - print(pformat_color_json(resp)) - - print("...Sleeping for 2 seconds...") - time.sleep(2) diff --git a/python/ray/serve/examples/echo_split.py b/python/ray/serve/examples/echo_split.py index 8de676525..19c66e355 100644 --- a/python/ray/serve/examples/echo_split.py +++ b/python/ray/serve/examples/echo_split.py @@ -2,12 +2,24 @@ Example of traffic splitting. We will first use echo:v1. Then v1 and v2 will split the incoming traffic evenly. """ +import json import time +from pygments import formatters, highlight, lexers + import requests from ray import serve -from ray.serve.utils import pformat_color_json + + +def pformat_color_json(d): + """Use pygments to pretty format and colorize dictionary""" + formatted_json = json.dumps(d, sort_keys=True, indent=4) + + colorful_json = highlight(formatted_json, lexers.JsonLexer(), + formatters.TerminalFormatter()) + + return colorful_json def echo_v1(_): diff --git a/python/ray/serve/utils.py b/python/ray/serve/utils.py index 9ee012c4c..a22430c39 100644 --- a/python/ray/serve/utils.py +++ b/python/ray/serve/utils.py @@ -10,7 +10,6 @@ import io import os import requests -from pygments import formatters, highlight, lexers import ray from ray.serve.constants import HTTP_PROXY_TIMEOUT @@ -79,16 +78,6 @@ class ServeEncoder(json.JSONEncoder): return super().default(o) -def pformat_color_json(d): - """Use pygments to pretty format and colroize dictionary""" - formatted_json = json.dumps(d, sort_keys=True, indent=4) - - colorful_json = highlight(formatted_json, lexers.JsonLexer(), - formatters.TerminalFormatter()) - - return colorful_json - - def block_until_http_ready(http_endpoint, backoff_time_s=1, timeout=HTTP_PROXY_TIMEOUT): diff --git a/python/setup.py b/python/setup.py index 8707f85a4..3122ae38c 100644 --- a/python/setup.py +++ b/python/setup.py @@ -83,7 +83,7 @@ if "RAY_USE_NEW_GCS" in os.environ and os.environ["RAY_USE_NEW_GCS"] == "on": extras = { "debug": [], "dashboard": ["requests", "gpustat"], - "serve": ["uvicorn", "flask", "blist"], + "serve": ["uvicorn", "flask", "blist", "requests"], "tune": ["tabulate", "tensorboardX", "pandas"] }