Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ray_demo

Demo FastAPI + Ray Client để chạy tác vụ song song và expose endpoint kiểm tra.

Yêu cầu

  • Docker (khuyến nghị) hoặc Python 3.12
  • Ray cluster chạy cục bộ (head node) và mở Ray Client

Khởi tạo Ray (trên máy host)

  1. Cài Ray CLI (nếu chưa có):
pip install -U "ray[default]"
  1. Khởi chạy head node kèm Ray Client và Dashboard:
ray stop  # tùy chọn, để dọn dẹp các phiên Ray cũ
ray start --head --port=6380 --dashboard-port=8265 --ray-client-server-port=10001

Chạy API

  • Dùng Docker (khuyến nghị trên macOS):
docker build -t ray-demo .
docker run --rm -p 8000:8000 ray-demo

Ghi chú:

  • Ứng dụng trong container kết nối Ray qua ray://host.docker.internal:10001 (đã cấu hình trong main.py).

  • Trên Linux, nếu cần, thêm: --add-host=host.docker.internal:host-gateway khi docker run.

  • Chạy local (không Docker):

pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 8000

Ghi chú: Nếu không dùng Docker, bạn có thể chỉnh địa chỉ trong main.py thành ray://127.0.0.1:10001 để kết nối Ray Client local.

Gọi API để kiểm tra

Sau khi API chạy ở http://localhost:8000, gọi endpoint sau:

curl http://localhost:8000/run-tasks

Kỳ vọng response dạng:

{
  "status": "Tasks completed",
  "results": [
    "Kết quả của task 0",
    "Kết quả của task 1",
    "..."
  ]
}

Logs trong server sẽ hiển thị quá trình các task chạy song song.

Kiểm tra Ray không qua API (tùy chọn)

Bạn có thể chạy trực tiếp script thử nghiệm:

python ray_test.py

Script này kết nối Ray ở 127.0.0.1:6380 và submit 10 task song song.

Dừng Ray cluster

ray stop

Cấu trúc chính

  • main.py: FastAPI với endpoint /run-tasks dùng Ray Client
  • ray_test.py: Script thử Ray trực tiếp (không qua API)
  • Dockerfile: Chạy FastAPI bằng Uvicorn
  • requirements.txt: Phụ thuộc Python (FastAPI, Uvicorn, Ray Client)

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages