终止 Actor#
在 Python 中,当所有 Actor 句柄副本超出作用域时,或当原始创建者进程死亡时,Actor 进程将自动终止。
请注意,Java 或 C++ 尚不支持 Actor 的自动终止。
通过 Actor 句柄手动终止#
在大多数情况下,Ray 会自动终止超出作用域的 Actor,但有时您可能需要强制终止 Actor。这仅适用于 Actor 意外挂起或资源泄漏的情况,以及必须手动销毁的分离 Actor。
import ray
@ray.remote
class Actor:
pass
actor_handle = Actor.remote()
ray.kill(actor_handle)
# This will not go through the normal Python sys.exit
# teardown logic, so any exit handlers installed in
# the actor using ``atexit`` will not be called.
actorHandle.kill();
// This will not go through the normal Java System.exit teardown logic, so any
// shutdown hooks installed in the actor using ``Runtime.addShutdownHook(...)`` will
// not be called.
actor_handle.Kill();
// This will not go through the normal C++ std::exit
// teardown logic, so any exit handlers installed in
// the actor using ``std::atexit`` will not be called.
这将导致 Actor 立即退出其进程,使得任何当前、待处理和未来的任务都因 RayActorError
而失败。如果您希望 Ray 自动重启 Actor,请确保在 Actor 的 @ray.remote
选项中设置一个非零的 max_restarts
,然后将标志 no_restart=False
传递给 ray.kill
。
对于命名和分离 Actor,在 Actor 句柄上调用 ray.kill
会销毁 Actor 并允许名称被重复使用。
使用 State API 中的 ray list actors --detail
查看死亡 Actor 的死因
# This API is only available when you download Ray via `pip install "ray[default]"`
ray list actors --detail
---
- actor_id: e8702085880657b355bf7ef001000000
class_name: Actor
state: DEAD
job_id: '01000000'
name: ''
node_id: null
pid: 0
ray_namespace: dbab546b-7ce5-4cbb-96f1-d0f64588ae60
serialized_runtime_env: '{}'
required_resources: {}
death_cause:
actor_died_error_context: # <---- You could see the error message w.r.t why the actor exits.
error_message: The actor is dead because `ray.kill` killed it.
owner_id: 01000000ffffffffffffffffffffffffffffffffffffffffffffffff
owner_ip_address: 127.0.0.1
ray_namespace: dbab546b-7ce5-4cbb-96f1-d0f64588ae60
class_name: Actor
actor_id: e8702085880657b355bf7ef001000000
never_started: true
node_ip_address: ''
pid: 0
name: ''
is_detached: false
placement_group_id: null
repr_name: ''
在 Actor 内部手动终止#
如有必要,您可以从 Actor 的方法内部手动终止 Actor。这将杀死 Actor 进程并释放与 Actor 关联/分配的资源。
@ray.remote
class Actor:
def exit(self):
ray.actor.exit_actor()
actor = Actor.remote()
actor.exit.remote()
通常不需要这种方法,因为 Actor 会自动进行垃圾回收。任务产生的 ObjectRef
可以被等待,以等待 Actor 退出(在其上调用 ray.get()
将引发 RayActorError
)。
Ray.exitActor();
Actor 的垃圾回收尚未实现,因此这是目前唯一优雅地终止 Actor 的方法。任务产生的 ObjectRef
可以被等待,以等待 Actor 退出(在其上调用 ObjectRef::get
将抛出 RayActorException
)。
ray::ExitActor();
Actor 的垃圾回收尚未实现,因此这是目前唯一优雅地终止 Actor 的方法。任务产生的 ObjectRef
可以被等待,以等待 Actor 退出(在其上调用 ObjectRef::Get
将抛出 RayActorException
)。
请注意,这种终止方法会等待所有先前提交的任务执行完毕,然后使用 sys.exit 优雅地退出进程。
您可以看到 Actor 由于用户调用 exit_actor()
而死亡
# This API is only available when you download Ray via `pip install "ray[default]"`
ray list actors --detail
---
- actor_id: 070eb5f0c9194b851bb1cf1602000000
class_name: Actor
state: DEAD
job_id: '02000000'
name: ''
node_id: 47ccba54e3ea71bac244c015d680e202f187fbbd2f60066174a11ced
pid: 47978
ray_namespace: 18898403-dda0-485a-9c11-e9f94dffcbed
serialized_runtime_env: '{}'
required_resources: {}
death_cause:
actor_died_error_context:
error_message: 'The actor is dead because its worker process has died.
Worker exit type: INTENDED_USER_EXIT Worker exit detail: Worker exits
by an user request. exit_actor() is called.'
owner_id: 02000000ffffffffffffffffffffffffffffffffffffffffffffffff
owner_ip_address: 127.0.0.1
node_ip_address: 127.0.0.1
pid: 47978
ray_namespace: 18898403-dda0-485a-9c11-e9f94dffcbed
class_name: Actor
actor_id: 070eb5f0c9194b851bb1cf1602000000
name: ''
never_started: false
is_detached: false
placement_group_id: null
repr_name: ''