Search code examples
pythonpyro

Calling a method of Pyro4 remote object stuck for ever.


Below is the example code to test this problem that I have. The execution just stuck forever when calling testmaster.test() which is a method of the server remote object (actually not sure here it is server or client).

Even @Pyro4.callback doesn't help either (not sure if it is logical to be here) I am using Python 2.7.12 and Pyro4 How can I solve this problem, any help will be appreciated

#Run python -m Pyro4.naming in another terminal first:
import Pyro4

@Pyro4.expose
@Pyro4.callback
class Master:
    @Pyro4.expose
    @Pyro4.callback
    def test(self):
        print "this is test"

nameserver = Pyro4.locateNS('localhost', 9090)
deamon = Pyro4.Daemon()
uri = deamon.register(Master())
nameserver.register("Master", uri, safe=True)
testmaster=Pyro4.Proxy(uri)#Object of master to call some functions from it
print "before calling test" #this will be executed
testmaster.test()
print "after calling test" #but not this, it just stuck forever- how can I make it to be executed
deamon.requestLoop()

Solution

  • You have to run the daemon in a separate process. That's whats Pyro is meant for: to call methods in other processes!

    Running it in the same process as the rest of your code makes no sense: why would you use Pyro at all then? You're not distributing your objects over different processes or machines.

    The reason your code 'hangs' is because the testmaster.test() call is trying to connect to a Pyro daemon but that is not running anywhere yet. So it will hang (until the socket times out). The daemon.requestLoop() is never reached - but still, even if it was, the code is wrong: it makes little sense to run a daemon and call objects via Pyro in the same program.

    I suggest reading the introduction in the manual at least to grasp the basics: http://pythonhosted.org/Pyro4/intro.html#simple-example