Pyro4: Remote object's attributes not changed?

340 Views Asked by At

I'm new to Pyro and pretty new to Python.

I have the following setup: 2 python scripts

Here is server.py:

import os
import sys

import Pyro4

class Packet(object):

    must_shutdown = False

    def __init__(self):
        self.some_attr = ""

    def close_connection(self):
        self.must_shutdown=True

def main():
    try:
        daemon = Pyro4.Daemon(host="localhost", port=65432)
        packet = Packet()
        uri = daemon.register(packet, "my.packet")

        print "Ready. Object uri =", uri

        daemon.requestLoop(loopCondition=lambda: not packet.must_shutdown)
        print "after loop"

    except Exception, e:
        print "Exception" + str(e)
    finally:
        print "Finally"

if __name__ == '__main__':
    main()

and this is client.py:

import os
import sys

import Pyro4

def main():
    try:

        packet = Pyro4.Proxy("PYRO:my.packet@localhost:65432")

        packet.some_attr = "this is client"

        packet.close_connection()

        print "client exiting"

    except Exception, e:
        print "Exception: " + str(e)
    finally:
        print "Finnally"

if __name__ == '__main__':
    main()

As you noticed, in server.py, in the requestloop there is a loopCondition "listening" for packet.must_shutdown to change to True. I start server.py and then client.py. The Pyro Packet object in client.py is obtained successfully. The problem is that even though I call close_connection method on the Pyro Packet object, thus setting must_shutdown to "True", the requestLoop does not break and the server still listening for requests. I log in close_connection and it is called but the interesting thing is that self.some_attr is still empty even thought it was set in client.py. It is like, somehow, client deals with another instance of Packet object than the one instantiated in server. What I'm missing here?

2

There are 2 best solutions below

1
On

Fixing the issues I mentioned int he comment will probably provide you with the answer.

Most notably; https://pyro4.readthedocs.io/en/stable/clientcode.html#accessing-remote-attributes where it says "you can access exposed attributes of your remote objects directly via the proxy." (emphasis added)

2
On

I found my issue in this thread which may be considered as duplicate: requestloop(loopCondition) doesn't release even after loopCondition is False. The problem was that I didn't set Pyro4.config.COMMTIMEOUT which by default is set to 0 and for some reason it doesn't care to check if requestLoop condition is satisfied or not. This is not specified in the requestLoop docs!

However I met a strange behavior which I consider a bug inside Pyro4.