Aygül Salahlı
Aygül Salahlı

Reputation: 83

AttributeError with Pyro4

Im trying to send an object with Pyro4. This is my server code :

class Player(object):

    def __init__(self, name='', clazz=C_SPEC):
        self.name = 'name'



class Game(object):
    def playeradd(self):
        '''Add spectator'''
        player = Player()
        ob = cPickle.dumps(player);
        return ob  

theGame = Game()

with Pyro4.core.Daemon() as daemon:
    uri = daemon.register(theGame)
    print uri
    daemon.requestLoop()

And my client looks like this:

     game = Pyro4.core.Proxy('PYRO:obj_ffbed0ab21894952ba941246fa5e1365@localhost:59639')
pl= cPickle.loads(str(game.playeradd())) 
print pl     

I get this error : AttributeError: 'module' object has no attribute 'Player'

Any opinion?

Upvotes: 1

Views: 791

Answers (1)

Irmen de Jong
Irmen de Jong

Reputation: 2847

You can't just unpickle the pickled object in your client code. As with all pickles, only the object's state is pickled and sent across the wire. To unpickle it, your code needs to have access to the same class in the same module as the object came from on the server side. I.e. you'll have to duplicate the module where your Player object is defined on both the client and the server.

However, I think you really want to achieve something else: it seems you want to create a spectating Player in your server and interact with it from your client code. This cannot be done in the way you're attempting here: the object in your client will be a copy of and be independent from the one you pickled in your server. You'll have to return a proxy instead of the actual object (or a pickle of it). But I suggest you look into Pyro4's autoproxy mechanism. See https://pythonhosted.org/Pyro4/servercode.html#autoproxying and also see the autoproxy example that comes with Pyro4.

Upvotes: 1

Related Questions