Basically I have this problem:
I use ZMQ to use a PUB/SUB
formal pattern to publish some pieces of data to other programs. First class, i.e. a Server waits for a client to .connect()
and when the client connects, Server sends the data to the Client.
I use ZMQ REQ/REP
to synchronize Server and Client.
This is Server.py
:
class PublishThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
socketPub.bind("tcp://127.0.0.1:4002")
syncservice.bind('tcp://127.0.0.1:5001')
def run(self):
while True:
subscribers = 0
topic = b"PHONEBOOK"
while subscribers < 1:
# Here we wait for client to connect. If the client doesn't
#connect, this is where this thread will "hang"
syncservice.recv()
# Send response to client
syncservice.send(b'')
subscribers += 1
socketPub.send_multipart([topic, data1])
subscribers = 0
Q1: How can I stop this thread when I want to exit my program?
Because when I want to exit my program, program "hangs" because thread is still running and waiting for a client.
Q2: How do you think that it will be best to implement this?
Q3: Is there any other way than REQ/REP
?