ray.serve.handle.DeploymentHandle#

class ray.serve.handle.DeploymentHandle[源代码]#

用于在运行时向部署发出请求的句柄。

这主要用于在单个应用程序中组合多个部署。它还可以用于调用应用程序的入口部署(例如,用于程序化测试)。

示例:

import ray
from ray import serve
from ray.serve.handle import DeploymentHandle, DeploymentResponse

@serve.deployment
class Downstream:
    def say_hi(self, message: str):
        return f"Hello {message}!"
        self._message = message

@serve.deployment
class Ingress:
    def __init__(self, handle: DeploymentHandle):
        self._downstream_handle = handle

    async def __call__(self, name: str) -> str:
        response = self._handle.say_hi.remote(name)
        return await response

app = Ingress.bind(Downstream.bind())
handle: DeploymentHandle = serve.run(app)
response = handle.remote("world")
assert response.result() == "Hello world!"

PublicAPI (测试版): 此API目前处于测试阶段,在成为稳定版本之前可能会发生变化。

options(*, method_name: str | DEFAULT = DEFAULT.VALUE, multiplexed_model_id: str | DEFAULT = DEFAULT.VALUE, stream: bool | DEFAULT = DEFAULT.VALUE, use_new_handle_api: bool | DEFAULT = DEFAULT.VALUE, _prefer_local_routing: bool | DEFAULT = DEFAULT.VALUE, _source: bool | DEFAULT = DEFAULT.VALUE) DeploymentHandle[源代码]#

为此句柄设置选项并返回更新后的副本。

示例:

response: DeploymentResponse = handle.options(
    method_name="other_method",
    multiplexed_model_id="model:v1",
).remote()
remote(*args, **kwargs) DeploymentResponse | DeploymentResponseGenerator[源代码]#

对部署的方法发出远程调用。

默认情况下,结果是一个 DeploymentResponse,可以等待以获取调用的结果,或传递给另一个 .remote() 调用以组合多个部署。

如果设置了 handle.options(stream=True) 并且调用了生成器方法,这将返回一个 DeploymentResponseGenerator

示例:

# Fetch the result directly.
response = handle.remote()
result = await response

# Pass the result to another handle call.
composed_response = handle2.remote(handle1.remote())
composed_result = await composed_response
参数:
  • *args – 要序列化并传递给远程方法调用的位置参数。

  • **kwargs – 要序列化并传递给远程方法调用的关键字参数。