mirror of
https://github.com/wassname/ray.git
synced 2026-07-01 22:06:00 +08:00
04f31db54d
* Convert UniqueID::nil() to a constructor * Cleanup actor handle pickling code * Add new actor handles to the task spec * Pass in new actor handles * Add new handles to the actor registration * Regression test for actor handle forking and GC * lint and doc * Handle pickled actor handles in the backend and some refactoring * Add regression test for dummy object GC and pickled actor handles * Check for duplicate actor tasks on submission * Regression test for forking twice, fix failed named actor leak * Fix bug for forking twice * lint * Revert "Fix bug for forking twice" This reverts commit 3da85e59d401e53606c2e37ffbebcc8653ff27ac. * Add new actor handles when task is assigned, not finished * Remove comment * remove UniqueID() * Updates * update * fix * fix java * fixes * fix
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
|
|
import ray
|
|
import ray.cloudpickle as pickle
|
|
from ray.experimental.internal_kv import _internal_kv_get, _internal_kv_put
|
|
|
|
|
|
def _calculate_key(name):
|
|
"""Generate a Redis key with the given name.
|
|
|
|
Args:
|
|
name: The name of the named actor.
|
|
|
|
Returns:
|
|
The key to use for storing a named actor in Redis.
|
|
"""
|
|
return b"Actor:" + name.encode("ascii")
|
|
|
|
|
|
def get_actor(name):
|
|
"""Get a named actor which was previously created.
|
|
|
|
If the actor doesn't exist, an exception will be raised.
|
|
|
|
Args:
|
|
name: The name of the named actor.
|
|
|
|
Returns:
|
|
The ActorHandle object corresponding to the name.
|
|
"""
|
|
actor_name = _calculate_key(name)
|
|
pickled_state = _internal_kv_get(actor_name)
|
|
if pickled_state is None:
|
|
raise ValueError("The actor with name={} doesn't exist".format(name))
|
|
handle = pickle.loads(pickled_state)
|
|
return handle
|
|
|
|
|
|
def register_actor(name, actor_handle):
|
|
"""Register a named actor under a string key.
|
|
|
|
Args:
|
|
name: The name of the named actor.
|
|
actor_handle: The actor object to be associated with this name
|
|
"""
|
|
if not isinstance(name, str):
|
|
raise TypeError("The name argument must be a string.")
|
|
if not isinstance(actor_handle, ray.actor.ActorHandle):
|
|
raise TypeError("The actor_handle argument must be an ActorHandle "
|
|
"object.")
|
|
actor_name = _calculate_key(name)
|
|
pickled_state = pickle.dumps(actor_handle)
|
|
|
|
# Add the actor to Redis if it does not already exist.
|
|
already_exists = _internal_kv_put(actor_name, pickled_state)
|
|
if already_exists:
|
|
# If the registration fails, then erase the new actor handle that
|
|
# was added when pickling the actor handle.
|
|
actor_handle._ray_new_actor_handles.pop()
|
|
raise ValueError(
|
|
"Error: the actor with name={} already exists".format(name))
|