Ray Tasks
Ray Tasks
Ray는 임의의 함수를 별도 워커 프로세스에서 비동기로 실행할 수 있게 해 줘요. 이런 함수를 Ray 원격 함수(remote function)라고 하고, 그 비동기 호출을 Ray 태스크(task)라고 해요. 파이썬 함수에 @ray.remote 데코레이터를 붙이기만 하면 클러스터 전반에 병렬로 실행할 수 있는 태스크가 돼요.
출처: Ray Tasks
기본 예시
파이썬에서 태스크는 이렇게 만들어요.
import ray
import time
# A regular Python function.
def normal_function():
return 1
# By adding the `@ray.remote` decorator, a regular Python function
# becomes a Ray remote function.
@ray.remote
def my_function():
return 1
# To invoke this remote function, use the `remote` method.
# This will immediately return an object ref (a future) and then create
# a task that will be executed on a worker process.
obj_ref = my_function.remote()
# The result can be retrieved with `ray.get`.
assert ray.get(obj_ref) == 1
@ray.remote
def slow_function():
time.sleep(10)
return 1
# Ray tasks are executed in parallel.
# All computation is performed in the background, driven by Ray's internal event loop.
for _ in range(4):
# This doesn't block.
slow_function.remote()
핵심을 정리하면 이래요.
@ray.remote데코레이터가 일반 파이썬 함수를 Ray 원격 함수로 바꿔요..remote()메서드로 호출하면 즉시 객체 참조(object ref, future)를 반환하고 워커에서 실행될 태스크를 만들어요.ray.get()으로 결과를 검색할 수 있어요.- 여러 태스크는 병렬로 실행되고,
slow_function.remote()호출은 블로킹하지 않아요.
Java에서는 일반 정적 메서드를 Ray.task(...).remote()로 호출해 태스크로 실행할 수 있고, C++에서는 RAY_REMOTE로 함수를 등록한 뒤 ray::Task(...).Remote()로 실행해요.
태스크 상태 확인
State API의 ray summary tasks를 사용하면 실행 중·완료된 태스크와 개수를 확인할 수 있어요.
# This API is only available when you download Ray via `pip install "ray[default]"`
ray summary tasks
출력은 현재 실행 중·완료된 태스크의 요약 표를 보여줘요.
더 알아보기
- What's Ray Core? — Ray Core 개념 워크스루
- Actors — 상태를 가진 워커
- Objects — 분산 객체 스토어
- Ray Overview — Ray 전체 구조