【ray】【Actors】【演员】

Ray Actors 是Ray API的扩展,允许将类实例作为有状态的工作者。每个Actor在自己的Python进程中运行,其方法按调用顺序串行执行并共享状态。你可以指定Actor的资源需求,通过操作符调用其方法,传递Actor句柄,甚至取消Actor任务。Ray的调度策略基于Actor的资源需求,而容错特性默认不重启意外崩溃的Actor。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.)
您也可以在执行元中指定资源要求(有关详细信息,请参阅任务或执行元资源要求)。

Python
# 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 来获取实际值。

Python
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

资源存储库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值