Nesredin Mahmud
Nesredin Mahmud

Reputation: 53

Pyro4: calling a remote method from a remote pyro object

I am attempting to call a Pyro object method p1.get_name() from another remote Pyro object p2. The method should return the name of the p1 object, but it is returning nothing (empty string). Surprisingly, I see that p1 is accessible from p2 because when I invoke p1.print_hello(), it works. It seems that a new instance is passed rather than the one initialized, I am not sure what is going on. Please have a look at the following code, thank you for your help!

The following piece of code creates the proxies (for readability, I ignored the name server and creating the daemons):

def create_proxy(ns_host, thing_host):
    ns = Pyro4.locateNS(host=ns_host)
    uri = ns.lookup(thing_host) 
    return Pyro4.Proxy(uri)

p1 = create_proxy('localhost', 'host1')
p1.init()
p2 = create_proxy('localhost', 'host2')
p2.init(p1)

The class definitions for p1 and p2 objects appear as follows:

Class Host1:
   def __init__(self)
      self.name = ''

   def init(sut):
      self.name = 'host 1'
   def get_name(self):
      return self.name

Class Host2:
   def init(p):
      print('Host name: ', p.get_name())

Cheers, /Nas

Upvotes: 0

Views: 248

Answers (1)

Irmen de Jong
Irmen de Jong

Reputation: 2847

No actual "instance" is passed via Pyro, what is passed is the proxy information to the remote object (in your case, p1).

However since the default server concurrency model is "session", Pyro will indeed create a new instance of Host1 when you eventually call get_name() from within the other Pyro object. There's no way to somehow "share" the same connection.

You can however share the same object if you're okay with making it a singleton (instancemode "single"). See the Pyro docs how to do this.

Upvotes: 1

Related Questions