[Twisted-Python] problem with numpy and twisted xmlrpc

Daniel Sank sank.daniel at gmail.com
Wed Apr 30 16:40:26 MDT 2014


Nicola,

Since you responded to my last message directly rather than to the list, I
am putting our conversation here for clarity. Please respond to the list
rather than to single users.

>>> I don't know whether this makes sense in the context of xmlrpc, but
perhaps
>>> you can use numpy's tostring and fromstring functions.
>>>

>> I dont know what is from/tostring. But this is another workaround that
add a
>> bug source. Infact it's probably that I'll forget some conversion
somewhere and
>> this will cause a time-expensive task searching where is the problem.
I'd like
>> to make twisted xmlrpc compatible with numpy without converting back and
forth
>> the data.
>> Thankyou
>>

> I get an exception cause probably twisted can't handle this type.

This is not a twisted problem. If you read the error message you put in
your original email you'll see near the end that it says

File "C:\Python27\lib\xmlrpclib.py", line 793, in close raise
Fault(**self._stack[0]) xmlrpclib.Fault: <Fault 8002: "Can't serialize
output: cannot marshal <type 'numpy.float64'> objects">

So you can see that the error is in xmlrpclib, and that the problem is that
marshal does not support numpy arrays.

> I dont know what is from/tostring.

Ok, are you only interested in solutions that use functions you already
know about?

I was going to explain how to deal with this, but since I see you posted an
identical question on stackoverflow I guess I'll go answer it there (since
this isn't a twisted problem after all).

Daniel

On Wed, Apr 30, 2014 at 11:13 AM, Daniel Sank <sank.daniel at gmail.com> wrote:

> Nicola,
>
> I don't know whether this makes sense in the context of xmlrpc, but
> perhaps you can use numpy's tostring and fromstring functions. If you do
> this you have to also send the datatype so that fromstring knows how to
> unpack (that's the part where I'm not sure if this strategy is xmlrpc-ish).
>
> -Daniel
>
>
>
> On Wed, Apr 30, 2014 at 10:48 AM, Nicola Cesca <nicola.cesca at mail.com>wrote:
>
>>  Hi all,
>> I'm newbie. I got a lot of problem using xmlrpc and numpy
>>
>>  I'm using numpy to to some staff and then serve the results via a
>> twisted/XMLRPC server. If the result is a numpy float 64, I get an
>> exception cause probably twisted can't handle this type. Infact is I
>> downgrade the result to float32 with x=float(x), everything is ok. This is
>> not so good cause if I forget this workaroud somewhere, it's a pain. Have
>> you any better solution?
>>
>> server:
>>
>> from twisted.web import xmlrpc, serverimport numpy as np
>> class MioServer(xmlrpc.XMLRPC):
>>     """
>>     An example object to be published.
>>     """
>>
>>     def xmlrpc_test_np(self):
>>         return np.sqrt(2)
>> if __name__ == '__main__':
>>     from twisted.internet import reactor
>>     r = MioServer()
>>     reactor.listenTCP(7080, server.Site(r))
>>     reactor.run()
>>
>> client:
>>
>> import xmlrpclib
>> if __name__=='__main__':
>>     x=xmlrpclib.ServerProxy('http://localhost:7080/')
>>     print x.test_np()
>>
>> Exception:
>>
>> Traceback (most recent call last):
>>   File "C:\Users\Stone\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1446, in <module>
>>     debugger.run(setup['file'], None, None)
>>   File "C:\Users\Stone\.eclipse\org.eclipse.platform_4.3.0_1709980481_win32_win32_x86\plugins\org.python.pydev_2.8.2.2013090511\pysrc\pydevd.py", line 1092, in run
>>     pydev_imports.execfile(file, globals, locals) #execute the script
>>   File "C:\Users\Stone\Documents\FastDose\src\Beagle\Prove e test\xmlrpc_client.py", line 28, in <module>
>>     print x.test_np()
>>   File "C:\Python27\lib\xmlrpclib.py", line 1224, in __call__
>>     return self.__send(self.__name, args)
>>   File "C:\Python27\lib\xmlrpclib.py", line 1578, in __request
>>     verbose=self.__verbose
>>   File "C:\Python27\lib\xmlrpclib.py", line 1264, in request
>>     return self.single_request(host, handler, request_body, verbose)
>>   File "C:\Python27\lib\xmlrpclib.py", line 1297, in single_request
>>     return self.parse_response(response)
>>   File "C:\Python27\lib\xmlrpclib.py", line 1473, in parse_response
>>     return u.close()
>>   File "C:\Python27\lib\xmlrpclib.py", line 793, in close
>>     raise Fault(**self._stack[0])
>> xmlrpclib.Fault: <Fault 8002: "Can't serialize output: cannot marshal <type 'numpy.float64'> objects">
>>
>>
>>
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>>
>>
>
>
> --
> Daniel Sank
> Department of Physics
> Broida Hall
> University of California
> Santa Barbara, CA 93117
> (805)893-3899
>



-- 
Daniel Sank
Department of Physics
Broida Hall
University of California
Santa Barbara, CA 93117
(805)893-3899
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20140430/974579ae/attachment-0002.html>


More information about the Twisted-Python mailing list