How do I make a dynamic list or array of ZeroMQ (pyzmq) sockets/contexts in Python?

15 views Asked by At

First off, please forgive me as I am a Python noob. I have a project where the best way to communicate with a series of remote devices is to connect to them with ZeroMQ in PAIR arrangement. (I'll spare the boring technical details as they are irrelevant to this problem.)

I am writing a Python routine that will create sockets for each device on a dedicated port.

Here is an offensively inelegant example of what I need to do:

import zmq

# Create device sockets like a pleb 
context = zmq.Context()
device1  = context.socket( zmq.SUB )
device1.connect(  "tcp://10.20.1.1:7771" )
device2 = context.socket( zmq.SUB )
device2.connect( "tcp://10.20.1.2:7772" )
device3 = context.socket( zmq.SUB )
device3.connect( "tcp://10.20.1.3:7773" )

In C++ I could do this with a dynamic array but I am not sure how this should be done in Python, especially when it comes to contexts. Thanks for your help!

0

There are 0 answers