[zeromq-dev] Change of type after sending over
Jon Dyte
jon at totient.co.uk
Mon Dec 26 10:32:17 CET 2011
On 26/12/11 06:27, Hor Meng Yoong wrote:
> Hi:
>
> import struct
> import time
> import zmq
> from zmq.eventloop import ioloop, zmqstream
> loop = ioloop.IOLoop.instance()
>
> ctx = zmq.Context()
> s = ctx.socket(zmq.REP)
> s.bind('tcp://127.0.0.1:5555 <http://127.0.0.1:5555>')
> stream = zmqstream.ZMQStream(s, loop)
>
> def echo(msg):
> print('receive', msg, len(msg))
> print('Type of ', msg, 'is ', type(msg))
> (a,), msg = struct.unpack('B', msg[:1]), msg[1:]
> print('a = %c ' % a)
> if (a == 'A'):
> stream.send(b'a')
> elif (a == 'B'):
> stream.send(b'b')
> else:
> stream.send(b'z')
>
> stream.on_recv(echo)
> loop.start()
>
> *Type of [b'ABCD'] is <class 'list'>*
>
> As the received msg is in list, I could not use struct.unpack:
> TypeError: 'list' does not support the buffer interface
>
> Is this a bug or a normal behavior for zmq send()/receive() to change
> the type?
>
> Regards
> Hor Meng
Hi
The type is not being changed.
The ZMQStream method on_recv calls the the callback ('echo') passing a
python list of all the messages received.
This is so that if a multipart message is sent the callback gets called
with all the message parts.
In the echo callback , you need to iterate over the list of messages passed.
It's described here:-
http://zeromq.github.com/pyzmq/api/generated/zmq.eventloop.zmqstream.html
Jon
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
More information about the zeromq-dev
mailing list