def deserialize(serialized_obj): """This is the callback that will be used by numbuf. If numbuf encounters a dictionary that contains the key "_pytype_" during deserialization, it will ask this callback to deserialize the object. Args: serialized_obj (object): A dictionary that contains the key "_pytype_". Returns: A Python object. """ class_id = serialized_obj["_pytype_"] cls = whitelisted_classes[class_id] if class_id in classes_to_pickle: obj = pickling.loads(serialized_obj["data"]) elif class_id in custom_deserializers.keys(): obj = custom_deserializers[class_id](serialized_obj["data"]) else: # In this case, serialized_obj should just be the __dict__ field. if "_ray_getnewargs_" in serialized_obj: obj = cls.__new__(cls, *serialized_obj["_ray_getnewargs_"]) else: obj = cls.__new__(cls) serialized_obj.pop("_pytype_") obj.__dict__.update(serialized_obj) return obj
def fetch_and_register_actor(actor_class_key, worker): """Import an actor. This will be called by the worker's import thread when the worker receives the actor_class export, assuming that the worker is an actor for that class. """ actor_id_str = worker.actor_id (driver_id, class_id, class_name, module, pickled_class, actor_method_names) = worker.redis_client.hmget(actor_class_key, [ "driver_id", "class_id", "class_name", "module", "class", "actor_method_names" ]) actor_name = class_name.decode("ascii") module = module.decode("ascii") actor_method_names = json.loads(actor_method_names.decode("ascii")) # Create a temporary actor with some temporary methods so that if the actor # fails to be unpickled, the temporary actor can be used (just to produce # error messages and to prevent the driver from hanging). class TemporaryActor(object): pass worker.actors[actor_id_str] = TemporaryActor() def temporary_actor_method(*xs): raise Exception("The actor with name {} failed to be imported, and so " "cannot execute this method".format(actor_name)) for actor_method_name in actor_method_names: function_id = get_actor_method_function_id(actor_method_name).id() worker.functions[driver_id][function_id] = (actor_method_name, temporary_actor_method) try: unpickled_class = pickling.loads(pickled_class) except Exception: # If an exception was thrown when the actor was imported, we record the # traceback and notify the scheduler of the failure. traceback_str = ray.worker.format_error_message(traceback.format_exc()) # Log the error message. worker.push_error_to_driver(driver_id, "register_actor", traceback_str, data={"actor_id": actor_id_str}) else: # TODO(pcm): Why is the below line necessary? unpickled_class.__module__ = module worker.actors[actor_id_str] = unpickled_class.__new__(unpickled_class) for (k, v) in inspect.getmembers( unpickled_class, predicate=(lambda x: (inspect.isfunction(x) or inspect.ismethod(x)))): function_id = get_actor_method_function_id(k).id() worker.functions[driver_id][function_id] = (k, v)
def fetch_and_register_actor(key, worker): """Import an actor.""" driver_id, actor_id_str, actor_name, module, pickled_class, class_export_counter = \ worker.redis_client.hmget(key, ["driver_id", "actor_id", "name", "module", "class", "class_export_counter"]) actor_id = photon.ObjectID(actor_id_str) actor_name = actor_name.decode("ascii") module = module.decode("ascii") class_export_counter = int(class_export_counter) try: unpickled_class = pickling.loads(pickled_class) except: raise NotImplemented("TODO(pcm)") else: # TODO(pcm): Why is the below line necessary? unpickled_class.__module__ = module worker.actors[actor_id_str] = unpickled_class.__new__(unpickled_class) for (k, v) in inspect.getmembers( unpickled_class, predicate=( lambda x: inspect.isfunction(x) or inspect.ismethod(x))): function_id = get_actor_method_function_id(k).id() worker.function_names[function_id] = k worker.functions[function_id] = v