首页 > 解决方案 > 在前台的 docker 容器中运行 ray 服务,而不是守护程序模式

问题描述

我正在运行 Ray Serve 来托管光线远程功能的 HTTP API。有没有比下面更好的方法在前台运行 Ray Serve(即不是守护程序模式)。代码直接取自 ray serve 示例:

import os
import time

import ray.serve

ray.serve.init(blocking=True, http_host="0.0.0.0", ray_init_kwargs={
    'webui_host': '0.0.0.0',
    'redis_password': os.getenv('RAY_REDIS_PASSWORD'),
})

ray.serve.create_endpoint("my_endpoint", "/echo")


def echo_v1(flask_request, response="hello from python!"):
    return "1"


ray.serve.create_backend(echo_v1, "echo:v1")

ray.serve.link("my_endpoint", "echo:v1")

# Make sure the Docker container doesn't exit
while True:
    time.sleep(2)

没有最后一部分:

# Make sure the Docker container doesn't exit
while True:
    time.sleep(2)

Docker 容器将立即退出。

标签: ray

解决方案


From the ray documentation as well (https://docs.ray.io/en/latest/serve/deployment.html), you can start a server beforehand with ray start --head and connect to the server with ray.init(address="auto"):

ray start --head
import ray
from ray import serve

# This will connect to the running Ray cluster.
ray.init(address="auto", namespace="serve")

@serve.deployment
def my_func(request):
  return "hello"

my_func.deploy()

推荐阅读