1

I am using ZeroMQ for data transmission between two processes: sender.py and receiver.py, and the communication pattern I'm using now is Publisher/Subscriber. The following is my code for both processes, respectively:

sender.py:

import zmq

context = zmq.Context()

publisher = context.socket(zmq.PUB)

# Set SNDHWM to not drop messages for slow subscribers
publisher.sndhwm = 1100000

publisher.bind("tcp://127.0.0.1:%s" % "5555")

for i in range(10000):
    publisher.send(i)

receiver.py

import zmq

context = zmq.Context()

subscriber = context.socket(zmq.SUB)
subscriber.connect("tcp://127.0.0.1:%s" % "5555")
subscriber.setsockopt(zmq.SUBSCRIBE, "")

while(True):
    data = subscriber.recv()

Actually, the code works fine. Currently, I want to do something on receiver.py: When received data > 1000 in receiver.py, it automatically terminates the running scripts for both receiver.py and sender.py. I wonder if I'm able to do that. I really appreciate for any suggestions or ideas :)

Ruofan Kong
  • 1,060
  • 1
  • 17
  • 34

2 Answers2

3

It's actually can be done, I'm not sure it is the correct pattern to your problem, but anyway:

You should use XSub and XPub, with XSUB you send subscription by calling send on the socket, but make sure you prefix the byte array with 0x1 byte. (0x0 to unsubscribe). In your case it will be one byte array set to 0x1.

On the XPUB side you need to receive all messages and process it, in case of messages prefixed with 0x0 or 0x1 just ignore them, the magic is with messages prefixed with different byte.

From XSUB when you want to send a message to the publisher which is not subscription or unsubscription just prefixed it with anything different than 0x0 or 0x1.

So for example when you get a message higher than 1000 send a message from xsub to xpub prefixed with 0x2. On XPUB when you receive a message prefixed with 2 kill the socket or whatever you want to do.

somdoron
  • 4,653
  • 2
  • 16
  • 24
2

Based on my very short experience with ZeroMQ.

Short answer: NO

Long answer: You can create additional countercurrent connection from SUB to PUB:

                   Node2
        |--------> SUB
  Node1 |<-------- PUB
   PUB->|
   SUB<-|          Node3
        |--------> SUB
        |<-------- PUB

Just use the possibility that PUB to SUB can transmit data in two configurations:

  • one to many (One PUB can talk to many SUB)

  • many to one (Many PUB can talk to one SUB)

http://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/patterns/pubsub.html

Marek
  • 862
  • 7
  • 19