1

In python I'm creating an application also using ZeroMQ. I'm using the PUSH/PULL method to send the loading status of one script to another. The message received on the PULL script runs inside of a Thread. The PULL script looks like this:

import time
from threading import Thread
import threading
import os
import zmq
import sys

context = zmq.Context()
zmqsocket = context.socket(zmq.PULL)
zmqsocket.bind("tcp://*:5555")

class TaskstatusUpdater(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        while True:
                #  Wait for next request from client
                task_id = int(zmqsocket.recv_multipart()[0])
                taskcolorstat = int(zmqsocket.recv_multipart()[1])
                taskstatus = zmqsocket.recv_multipart()[2]
                time.sleep(0.1)
                print(task_id, taskstatus, taskcolorstat)

thread = TaskstatusUpdater()
thread.start()

The PUSH part sends constantly updates about the status of the other script. It looks something like this:

import time
import sys
import zmq

# zmq - client startup and connecting

try:
    context = zmq.Context()
    print("Connecting to server…")
    zmqsocket = context.socket(zmq.PUSH)
    zmqsocket.connect("tcp://localhost:5555")
    print("succesful")
except:
    print('error could not connect to service')

# zmq - client startup and connecting

for i in range(10):
    zmqsocket.send_multipart([b_task_id, b"0", b"first message"])
    time.sleep(3)# doing stuff
    zmqsocket.send_multipart([b_task_id, b"1", b"second message"])

b_task_id is generated earlier in the program and is a simple binary value created out of an integer. There are multiple of those PUSH scripts running at the same time and thru the b_task_id I can define which script is responding to the PULL.

It is now often the case that those multipart messages get mixed up between each other. Can somebody explain to me why that is and how I can fix this problem? For example, sometimes the output is:

2 b'second message' 0

The output that I was expecting is:

2 b'second message' 1
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Gordian
  • 101
  • 8

0 Answers0