1

I have a module called foobar and it contains a bunch of functions I would like to call remotely.

My current solution is to wrap all those functions as static methods in a class and share that.

Here's my code:

pyro_server.py:

import Pyro4
import foobar

import inspect
Pyro4.config.REQUIRE_EXPOSE = False

import my_custom_pyro_config as pyro_config

def module_to_class(module):
    class Wrapper:
        pass
    for name, func in inspect.getmembers(module, inspect.isfunction):
        setattr(Wrapper, name, staticmethod(func))
    return Wrapper

def main():
    name_server = Pyro4.locateNS(host=pyro_config.IP, port=pyro_config.NS_PORT)
    daemon = Pyro4.Daemon(host=pyro_config.IP, port=pyro_config.PYRO_PORT)
    foobar_uri = daemon.register(module_to_class(foobar))
    name_server.register("foobar", foobar_uri)
    print("Entering request loop")
    daemon.requestLoop()

It works, but it feels kind of dodgy...

Is there a better way to do this? I'm open to switching to another RPC library

user357269
  • 1,835
  • 14
  • 40

1 Answers1

1

Use Pyro's "Flame" feature. It allows direct remote access to a module on the server, without having to expose the members manually.

>>> import Pyro4.utils.flame
>>> Pyro4.config.SERIALIZER="pickle"
>>> fl = Pyro4.utils.flame.connect("localhost:55225")  # use whatever location the flame server is running on
>>> s = fl.module("sys")
>>> s.stdout.write("foobar\n")
7    
# ...and observe 'foobar' being written to the server's output
Irmen de Jong
  • 2,739
  • 1
  • 14
  • 26