The problem is probably that you need to implement locking in send_to_master
to ensure that only one pair socket is opened at a time across your application.
You could try implementing locking as detailed at: https://stackoverflow.com/a/10181810/288425
Here is a stab, though I haven't tested it:
from threading import Lock
lock = Lock()
def send_to_master(msg):
context = zmq.Context()
with lock:
s = context.socket(zmq.PAIR)
s.connect("tcp://localhost:9001")
s.send(msg)
# result received from the service
data = s.recv()
s.close()
return data
...