简体   繁体   English

Pyro4的AttributeError

[英]AttributeError with Pyro4

Im trying to send an object with Pyro4. 我正在尝试使用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' 我收到此错误:AttributeError:'模块'对象没有属性'Player'

Any opinion? 有什么意见吗?

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. 要释放它,您的代码需要访问与该对象来自服务器端相同模块中的同一类。 Ie you'll have to duplicate the module where your Player object is defined on both the client and the server. 也就是说,您必须复制客户端和服务器上都定义了Player对象的模块。

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. 但是,我认为您确实想实现其他目标:似乎要在服务器中创建一个旁观的Player,并通过客户端代码与之交互。 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. 但我建议你看看Pyro4的自动代理机制。 See https://pythonhosted.org/Pyro4/servercode.html#autoproxying and also see the autoproxy example that comes with Pyro4. 请参阅https://pythonhosted.org/Pyro4/servercode.html#autoproxying ,另请参阅Pyro4随附的autoproxy示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM