python-2.7pyro

Pyro4 pickle serializer numpy array


Trying to serialize numpy array with Pyro4 is returning the following type error

TypeError: don't know how to serialize class <type 'numpy.ndarray'>. Give it vars() or an appropriate __getstate__

The code is a the following

import numpy as np
import Pyro4

# set pickle serializer
Pyro4.config.SERIALIZERS_ACCEPTED = set(['pickle','json', 'marshal', 'serpent'])


@Pyro4.expose
class test(object):
    def get_array(self):
        return np.random.random((10,10))


def main():
    # create a Pyro daemon
    daemon = Pyro4.Daemon()    
    # register test instance
    uri = daemon.register(test())
    # print uri to connect to it in another console
    print uri
    # start the event loop of the server to wait for calls
    daemon.requestLoop()                   

if __name__=="__main__":
    main()

now open another console and try to call test instance doing the following

import Pyro4
Pyro4.config.SERIALIZERS_ACCEPTED = set(['pickle','json', 'marshal', 'serpent'])
# connect to URI which is printed above
# must be something like this 'PYRO:obj_c261949088104b839878255b98a9da90@localhost:57495'
p = Pyro4.Proxy(URI)
p.get_array()


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/Pyro4/core.py", line 171, in __call__
    return self.__send(self.__name, args, kwargs)
  File "/usr/local/lib/python2.7/site-packages/Pyro4/core.py", line 438, in _pyroInvoke
    raise data
TypeError: don't know how to serialize class <type 'numpy.ndarray'>. Give it vars() or an appropriate __getstate

Solution

  • This is mentioned in the manual including what you can do to solve it: http://pythonhosted.org/Pyro4/tipstricks.html#pyro-and-numpy

    In your code above, you didn't tell your client code to use pickle. You should use another config item for that (SERIALIZER). What you have there is meant for Pyro deamons instead.