Skip to content

Commit

Permalink
[serve] Add code example for get_deployment_handle api (ray-project…
Browse files Browse the repository at this point in the history
  • Loading branch information
zcin authored Oct 25, 2023
1 parent 7f98a20 commit 8f9069e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions python/ray/serve/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,62 @@ def get_deployment_handle(
RayServeException: If no Serve controller is running, or if
calling from outside a Serve application and no application
name is specified.
The following example gets the handle to the ingress deployment of
an application, which is equivalent to using `serve.get_app_handle`.
.. testcode::
import ray
from ray import serve
@serve.deployment
def f(val: int) -> int:
return val * 2
serve.run(f.bind(), name="my_app")
handle = serve.get_deployment_handle("f", app_name="my_app")
assert handle.remote(3).result() == 6
serve.shutdown()
The following example demonstrates how you can use this API to get
the handle to a non-ingress deployment in an application.
.. testcode::
import ray
from ray import serve
from ray.serve.handle import DeploymentHandle
@serve.deployment
class Multiplier:
def __init__(self, multiple: int):
self._multiple = multiple
def __call__(self, val: int) -> int:
return val * self._multiple
@serve.deployment
class Adder:
def __init__(self, handle: DeploymentHandle, increment: int):
self._handle = handle.options(use_new_handle_api=True)
self._increment = increment
async def __call__(self, val: int) -> int:
return await self._handle.remote(val) + self._increment
# The app calculates 2 * x + 3
serve.run(Adder.bind(Multiplier.bind(2), 3), name="math_app")
handle = serve.get_app_handle("math_app")
assert handle.remote(5).result() == 13
# Get handle to Multiplier only
handle = serve.get_deployment_handle("Multiplier", app_name="math_app")
assert handle.remote(5).result() == 10
serve.shutdown()
"""

client = _get_global_client()
Expand Down

0 comments on commit 8f9069e

Please sign in to comment.