[zeromq-dev] Change of type after sending over

Hor Meng Yoong yoonghm at gmail.com
Mon Dec 26 10:58:40 CET 2011


Hi Jon:

  I have fixed it. Thank.

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')
stream = zmqstream.ZMQStream(s, loop)

def echo(msg):
  msg = msg[0]
  (a,), msg = struct.unpack('>B', msg[:1]), msg[1:]
  print('a=', a, 'type=', type(a))
  if (a == 1):
    stream.send(b'Got it')
  else:
    stream.send(b'No no')

def sigalrm_handler():
  print(time.time())

beat = ioloop.PeriodicCallback(sigalrm_handler, 2000)
beat.start()
stream.on_recv(echo)
loop.start()


On Mon, Dec 26, 2011 at 5:32 PM, Jon Dyte <jon at totient.co.uk> wrote:

> 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
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20111226/1dd1841a/attachment.htm>


More information about the zeromq-dev mailing list