-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prototype named actors. #2129
Prototype named actors. #2129
Changes from all commits
2180f0e
1a86d3f
aefd107
e72f278
a29098a
40d1b03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's reserve assertions for checking things that should never actually happen. The errors (in both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handled. |
||
import ray | ||
import ray.cloudpickle as pickle | ||
|
||
|
||
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. | ||
""" | ||
worker = ray.worker.get_global_worker() | ||
actor_hash = _calculate_key(name) | ||
pickled_state = worker.redis_client.hget(actor_hash, 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 | ||
""" | ||
worker = ray.worker.get_global_worker() | ||
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_hash = _calculate_key(name) | ||
pickled_state = pickle.dumps(actor_handle) | ||
|
||
# Add the actor to Redis if it does not already exist. | ||
updated = worker.redis_client.hsetnx(actor_hash, name, pickled_state) | ||
if updated == 0: | ||
raise ValueError( | ||
"Error: the actor with name={} already exists".format(name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this change needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm.. ok I see that the test failures without it.