forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serve][Doc] Rewrite the ServeHandle page (ray-project#27775)
Signed-off-by: Stefan van der Kleij <[email protected]>
- Loading branch information
Showing
4 changed files
with
152 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,119 @@ | ||
# flake8: noqa | ||
|
||
# __begin_sync_handle__ | ||
import ray | ||
from ray import serve | ||
import requests | ||
from ray.serve.handle import RayServeSyncHandle | ||
|
||
|
||
# __basic_example_start__ | ||
@serve.deployment | ||
class Deployment: | ||
def method1(self, arg): | ||
return f"Method1: {arg}" | ||
class Model: | ||
def __call__(self): | ||
return "hello" | ||
|
||
def __call__(self, arg): | ||
return f"__call__: {arg}" | ||
|
||
handle: RayServeSyncHandle = serve.run(Model.bind()) | ||
ref: ray.ObjectRef = handle.remote() # blocks until request is assigned to replica | ||
assert ray.get(ref) == "hello" | ||
# __end_sync_handle__ | ||
|
||
handle = serve.run(Deployment.bind()) | ||
# __begin_async_handle__ | ||
import asyncio | ||
import random | ||
import ray | ||
from ray import serve | ||
from ray.serve.handle import RayServeDeploymentHandle, RayServeSyncHandle | ||
|
||
ray.get(handle.remote("hi")) # Defaults to calling the __call__ method. | ||
ray.get(handle.method1.remote("hi")) # Call a different method. | ||
# __basic_example_end__ | ||
|
||
@serve.deployment | ||
class Model: | ||
def __call__(self): | ||
return "hello" | ||
|
||
# __async_handle_start__ | ||
@serve.deployment(route_prefix="/api") | ||
class Deployment: | ||
def say_hello(self, name: str): | ||
return f"Hello {name}!" | ||
|
||
def __call__(self, request): | ||
return self.say_hello(request.query_params["name"]) | ||
@serve.deployment | ||
class DynamicDispatcher: | ||
def __init__( | ||
self, handle_a: RayServeDeploymentHandle, handle_b: RayServeDeploymentHandle | ||
): | ||
self.handle_a = handle_a | ||
self.handle_b = handle_b | ||
|
||
async def __call__(self): | ||
handle_chosen = self.handle_a if random.random() < 0.5 else self.handle_b | ||
|
||
# The request is enqueued. | ||
submission_task: asyncio.Task = handle_chosen.remote() | ||
# The request is assigned to a replica. | ||
ref: ray.ObjectRef = await submission_task | ||
# The request has been processed by the replica. | ||
result = await ref | ||
|
||
return result | ||
|
||
handle = serve.run(Deployment.bind()) | ||
|
||
# __async_handle_end__ | ||
handle: RayServeSyncHandle = serve.run( | ||
DynamicDispatcher.bind(Model.bind(), Model.bind()) | ||
) | ||
ref: ray.ObjectRef = handle.remote() | ||
assert ray.get(ref) == "hello" | ||
|
||
# __end_async_handle__ | ||
|
||
# __begin_async_handle_chain__ | ||
import asyncio | ||
import ray | ||
from ray import serve | ||
from ray.serve.handle import RayServeDeploymentHandle, RayServeSyncHandle | ||
|
||
# __async_handle_print_start__ | ||
print(requests.get("http:https://localhost:8000/api?name=Alice")) | ||
# Hello Alice! | ||
|
||
print(ray.get(handle.say_hello.remote("Alice"))) | ||
# Hello Alice! | ||
# __async_handle_print_end__ | ||
@serve.deployment | ||
class Model: | ||
def __call__(self, inp): | ||
return "hello " + inp | ||
|
||
|
||
@serve.deployment | ||
class Chain: | ||
def __init__( | ||
self, handle_a: RayServeDeploymentHandle, handle_b: RayServeDeploymentHandle | ||
): | ||
self.handle_a = handle_a | ||
self.handle_b = handle_b | ||
|
||
async def __call__(self, inp): | ||
ref: asyncio.Task = await self.handle_b.remote( | ||
# Serve can handle enqueued-task as dependencies. | ||
self.handle_a.remote(inp) | ||
) | ||
return await ref | ||
|
||
|
||
handle: RayServeSyncHandle = serve.run(Chain.bind(Model.bind(), Model.bind())) | ||
ref: ray.ObjectRef = handle.remote("Serve") | ||
assert ray.get(ref) == "hello hello Serve" | ||
|
||
# __end_async_handle_chain__ | ||
|
||
|
||
# __begin_handle_method__ | ||
import ray | ||
from ray import serve | ||
from ray.serve.handle import RayServeSyncHandle | ||
|
||
|
||
@serve.deployment | ||
class Deployment: | ||
def method1(self, arg): | ||
return f"Method1: {arg}" | ||
|
||
def __call__(self, arg): | ||
return f"__call__: {arg}" | ||
|
||
|
||
handle: RayServeSyncHandle = serve.run(Deployment.bind()) | ||
|
||
ray.get(handle.remote("hi")) # Defaults to calling the __call__ method. | ||
ray.get(handle.method1.remote("hi")) # Call a different method. | ||
|
||
# __end_handle_method__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters