0

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
Cobry
  • 4,348
  • 8
  • 33
  • 49

1 Answers1

1

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.

Irmen de Jong
  • 2,739
  • 1
  • 14
  • 26
  • I've been here before but I want to change the config from within the code and not change PYRO_SERIALIZERS_ACCEPTED system global config variable. I am new to pyro sorry for the stupid questions. – Cobry Oct 20 '16 at 17:24
  • 2
    Every config item (except for the logging-related) can be set both via an environment variable or via a code change. Note that environment variables are usually not system-global but per-user. Anyway, you should have set Pyro4.config.SERIALIZER='pickle' – Irmen de Jong Oct 21 '16 at 12:35