Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an existing dict through zmq ipc

Tags:

python

zeromq

I'm trying to send an existing dict through zmq ipc socket, I can send a string with this code, but I can't send a dict object

import zmq, datetime

d = {0: ('356612022462768', 'EVENT', 0, '2012-12-26 15:50:16', -20.22216, -70.13723, 6.44, 134.0, 1, 2, '18743230', datetime.datetime(2013, 2, 10, 9, 6, 2, 362734))}

if __name__ == "__main__":
    context = zmq.Context()
    publisher = context.socket(zmq.PUB)
    publisher.connect("ipc://shared")
    while True:
        publisher.send( d )
        time.sleep( 1 )

TypeError: {0: ('356612022462768', 'EVENT', 0, '2012-12-26 15:50:16', 
           -20.22216, -70.13723, 6.44, 134.0, 1, 2, '18743230', 
           datetime.datetime(2013, 2, 10, 9, 6, 2, 362734))} 
does not provide a buffer interface.

How can I do that?

like image 963
chespinoza Avatar asked Feb 10 '13 12:02

chespinoza


People also ask

What is ZMQ socket?

ZeroMQ (also known as ØMQ, 0MQ, or zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast.

What is ZeroMQ used for?

ZeroMQ provides a whole slew of language APIs which run on most operating systems and allows you to communicate seamlessly between all sorts of programs. It also provides a collection of patterns, such as request-reply and publish-subscribe which assist you in creating and structuring your network.

What is a ZMQ context?

Contexts help manage any sockets that are created as well as the number of threads ZeroMQ uses behind the scenes. Create one when you initialize a process and destroy it as the process is terminated. Contexts can be shared between threads and, in fact, are the only ZeroMQ objects that can safely do this.

What is ZMQ Python?

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker.


1 Answers

It is only possible to either send strings or byte arrays through ZeroMq, out-of-the-box. Anything else needs to be serialized before passing it to ZeroMq for transfer on the wire.

You can use whatever you like to serialize it, for example Protocol Buffers, JSON or Message Pack. Note that any recipients needs to be able to deserialize the data using the same protocol.

You can find examples on how to use various serialization techniques (including a numpy array) in conjunction with pyzmq here. They are part of the pyzmq source.

like image 142
Jakob Möllås Avatar answered Oct 12 '22 14:10

Jakob Möllås