def test(self): exp = export_function(f) f2 = import_function(exp) self.assertEqual(f(6)(7), f2(6)(7)) h2 = teleport_function(self.conn, h) self.assertNotEqual(h(7), h2(7))
def teleport_function(conn, func, globals=None, def_=True): """ "Teleports" a function (including nested functions/closures) over the RPyC connection. The function is passed in bytecode form and reconstructed on the other side. The function cannot have non-brinable defaults (e.g., ``def f(x, y=[8]):``, since a ``list`` isn't brinable), or make use of non-builtin globals (like modules). You can overcome the second restriction by moving the necessary imports into the function body, e.g. :: def f(x, y): import os return (os.getpid() + y) * x .. note:: While it is not forbidden to "teleport" functions across different Python versions, it *may* result in errors due to Python bytecode differences. It is recommended to ensure both the client and the server are of the same Python version when using this function. :param conn: the RPyC connection :param func: the function object to be delivered to the other party """ if globals is None: globals = conn.namespace from rpyc.utils.teleportation import export_function exported = export_function(func) return conn.modules["rpyc.utils.teleportation"].import_function( exported, globals, def_)
def test(self): exp = export_function(f) f2 = import_function(exp) self.assertEqual(f(6)(7), f2(6)(7)) # HACK: needed so the other side could import us (for globals) mod = self.conn.modules.types.ModuleType(__name__) self.conn.modules.sys.modules[__name__] = mod mod.__builtins__ = self.conn.builtins h2 = teleport_function(self.conn, h) self.assertNotEqual(h(7), h2(7))
def teleport_function(conn, func): """ "Teleports" a function (including nested functions/closures) over the RPyC connection. The function is passed in bytecode form and reconstructed on the other side. The function cannot have non-brinable defaults (e.g., ``def f(x, y=[8]):``, since a ``list`` isn't brinable), or make use of non-builtin globals (like modules). You can overcome the second restriction by moving the necessary imports into the function body, e.g. :: def f(x, y): import os return (os.getpid() + y) * x :param conn: the RPyC connection :param func: the function object to be delivered to the other party """ from rpyc.utils.teleportation import export_function exported = export_function(func) return conn.modules["rpyc.utils.teleportation"].import_function(exported)
def set_to_run(self, new_to_run): self._to_run = export_function(new_to_run)