from Pyro5.api import expose, Proxy, register_dict_to_class from customdata import CustomData # teach the serializer how to deserialize our custom data class register_dict_to_class(CustomData.serialized_classname, CustomData.from_dict) class Listener(object): def __init__(self, topic): self.topic = topic def register_with_dispatcher(self): with Proxy("PYRONAME:example.blobdispatcher") as dispatcher: dispatcher.register(self.topic, self) @expose def process_blob(self, blob): assert blob.info == self.topic customdata = blob.deserialized() print("Received custom data (type={}):".format(type(customdata))) print(" a={}, b={}, c={}".format(customdata.a, customdata.b, customdata.c))
"__class__": "Pyro5.utils.messagebus.message", "msgid": str(obj.msgid), "created": obj.created.isoformat(), "data": obj.data } @classmethod def from_dict(cls, classname, d): return cls(uuid.UUID(d["msgid"]), datetime.datetime.strptime(d["created"], "%Y-%m-%dT%H:%M:%S.%f"), d["data"]) # make sure Pyro knows how to serialize the custom Message class register_class_to_dict(Message, Message.to_dict) register_dict_to_class("Pyro5.utils.messagebus.message", Message.from_dict) @expose class Subscriber(object): def __init__(self, auto_consume=True, max_queue_size=5000): self.bus = Proxy("PYRONAME:"+PYRO_MSGBUS_NAME) self.received_messages = queue.Queue(maxsize=max_queue_size) if auto_consume: self.__bus_consumer_thread = threading.Thread(target=self.__bus_consume_message) self.__bus_consumer_thread.daemon = True self.__bus_consumer_thread.start() def incoming_message(self, topic, message): self.received_messages.put((topic, message))
} def thingy_dict_to_class(classname, d): print("{deserializer hook, converting to class: %s}" % d) return mycustomclasses.Thingy(d["number-attribute"]) def otherthingy_dict_to_class(classname, d): print("{deserializer hook, converting to class: %s}" % d) return mycustomclasses.OtherThingy(d["number"]) # for 'Thingy' we register both serialization and deserialization hooks register_class_to_dict(mycustomclasses.Thingy, thingy_class_to_dict) register_dict_to_class("waheeee-custom-thingy", thingy_dict_to_class) # for 'OtherThingy' we only register a deserialization hook (and for serialization depend on serpent's default behavior) register_dict_to_class("mycustomclasses.OtherThingy", otherthingy_dict_to_class) # regular pyro stuff uri = input("Enter the URI of the server object: ") serv = Proxy(uri) print("\nTransferring thingy...") o = mycustomclasses.Thingy(42) response = serv.method(o) print("type of response object:", type(response)) print("response:", response) print("\nTransferring otherthingy...") o = mycustomclasses.OtherThingy(42)
import queue from Pyro5.api import expose, behavior, serve, register_dict_to_class from workitem import Workitem # For 'workitem.Workitem' we register a deserialization hook to be able to get these back from Pyro register_dict_to_class("workitem.Workitem", Workitem.from_dict) @expose @behavior(instance_mode="single") class DispatcherQueue(object): def __init__(self): self.workqueue = queue.Queue() self.resultqueue = queue.Queue() def putWork(self, item): self.workqueue.put(item) def getWork(self, timeout=5): try: return self.workqueue.get(block=True, timeout=timeout) except queue.Empty: raise ValueError("no items in queue") def putResult(self, item): self.resultqueue.put(item) def getResult(self, timeout=5): try: return self.resultqueue.get(block=True, timeout=timeout) except queue.Empty:
def world_update(self, iteration, world, robotdata): if iteration % 50 == 0: self.robot._pyroClaimOwnership() # lets our thread do the proxy calls self.robot.emote("I'm scared!") observers = { "drunk": DrunkenGameObserver, "angry": AngryGameObserver, "scared": ScaredGameObserver, } # register the Robot class with Pyro's serializers: register_class_to_dict(robot.Robot, robot.Robot.robot_to_dict) register_dict_to_class("robot.Robot", robot.Robot.dict_to_robot) def main(args): if len(args) != 3: print("usage: client.py <robotname> <robottype>") print(" type is one of: %s" % list(observers.keys())) return name = args[1] observertype = args[2] with Daemon() as daemon: observer = observers[observertype]() daemon.register(observer) gameserver = Proxy("PYRONAME:example.robotserver") robot = gameserver.register(name, observer) with robot: # make sure it disconnects, before the daemon thread uses it later
# This import needs to be absolute to reconstruct the Exception class. Note that the # eval is safe here because ``serpent_deserialize_api_error`` is only registered for # strings that match an error class name. import maestral.errors # noqa: F401 cls = eval(class_name) err = cls(*d["args"]) for a_name, a_value in d["attributes"].items(): setattr(err, a_name, a_value) return err for err_cls in (*SYNC_ERRORS, *GENERAL_ERRORS): register_dict_to_class(err_cls.__module__ + "." + err_cls.__name__, serpent_deserialize_api_error) # ==== interprocess locking ============================================================ def _get_lockdata() -> Tuple[bytes, str, int]: try: os.O_LARGEFILE except AttributeError: start_len = "ll" else: start_len = "qq" if sys.platform.startswith(("netbsd", "freebsd", "openbsd")) or IS_MACOS: if struct.calcsize("l") == 8: