Actors 演员
Actors extend the Ray API from functions (tasks) to classes. An actor is essentially a stateful worker (or a service). When a new actor is instantiated, a new worker is created, and methods of the actor are scheduled on that specific worker and can access and mutate the state of that worker.
Actor将Ray API从函数(任务)扩展到类。Actor本质上是一个有状态的工作者(或服务)。当一个新的参与者被实例化时,一个新的工作者被创建,参与者的方法被安排在这个特定的工作者上,并且可以访问和改变这个工作者的状态。
The ray.remote
decorator indicates that instances of the Counter
class will be actors. Each actor runs in its own Python process. ray.remote
装饰器表示 Counter
类的实例将是actors。每个actor都在自己的Python进程中运行。
import ray
@ray.remote
class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
return self.value
def get_counter(self):
return self.value
# Create an actor from this class.
counter = Counter.remote()
Use ray list actors
from State API to see actors states:
使用State API中的 ray list actors
查看参与者状态:
# This API is only available when you install Ray with `pip install "ray[default]"`. ray list actors
======== List: 2023-05-25 10:10:50.095099 ======== Stats: ------------------------------ Total: 1 Table: ------------------------------ ACTOR_ID CLASS_NAME STATE JOB_ID NAME NODE_ID PID RAY_NAMESPACE 0 9e783840250840f87328c9f201000000 Counter ALIVE 01000000 13a475571662b784b4522847692893a823c78f1d3fd8fd32a2624923 38906 ef9de910-64fb-4575-8eb5-50573faa3ddf
1 Specifying required resources
1 指定所需资源
You can specify resource requirements in actors too (see Specifying Task or Actor Resource Requirements for more details.)
您也可以在执行元中指定资源要求(有关详细信息,请参阅任务或执行元资源要求)。
# Specify required resources for an actor.
@ray.remote(num_cpus=2, num_gpus=0.5)
class Actor:
pass
2 Calling the actor
2 调用 Actor
We can interact with the actor by calling its methods with the remote
operator. We can then call get
on the object ref to retrieve the actual value.
我们可以通过使用 remote
操作符调用它的方法来与actor交互。然后我们可以在对象ref上调用 get
来获取实际值。