Search code examples
pythonpyro

Pyro4, serve object with constructor parameters. how?


I have an object that takes a parameter in the constructor. I was wondering how I can serve this from Pyro4. An Example:

import Pyro4

class MyPyroThing(object):
    def __init__(self, theNumber):
        self.Number = theNumber

Pyro4.Daemon.serveSimple(
    {
        MyPyroThing(): None
    },
    ns=True, verbose=True)

This fails of course because the constructor must have a parameter.

And when this is solved, how do you invoke such object?

theThing = Pyro4.Proxy("PYRONAME:MyPyroThing")

EDIT:

I think this question was not written correctly, see my answer below.


Solution

  • The answers above where not what I was really asking, meaning I explained my question badly. Mea Culpa.

    I wanted to invoke an instance on the client. But that is not how Pyro4 works at all. A class in instantiated on the server and this instance is transmitted over the wire.

    After mailing Irmin (the original developer) it came clear to me how Pyro4 works.

    So, what I do now is use a factory pattern where I ask the factory to give me an instance of an object. For instance:

    psf = Pyro4.Proxy("PYRONAME:MyApp.Factories.ProductFactory")
    product = psf.GetProductOnButton(buttonNoPressed, parentProductId)
    

    product is an instance of the Product() class. Because the instance is registered in the Pyro daemon, i can call methods on this instance of Product() too. Look at the shoppingcart example to know where I got my eureka moment.