python zeromq pub-sub simple example

Hyunchul·2021년 8월 27일
0

Pub


import zmq 
import time
context = zmq.Context() # Socket to talk to server 
print("Connecting to hello world server…") 
socket = context.socket(zmq.PUB) 
socket.bind("tcp://*:5555") 
t0 = time.time()
while True: # Wait for next request from client 
    socket.send(b"f") # Get the reply. 
    # message = socket.recv() 
    time.sleep(0.5)
    if time.time()-t0 > 20:
        break

Sub


import time 
import zmq 

context = zmq.Context() 
socket = context.socket(zmq.SUB) 
socket.connect("tcp://localhost:5555") 
t0 = time.time()
socket.subscribe("")
while True: # Wait for next request from client 
    message = socket.recv()
    # print("Received request: %s" % message) # Do some 'work' 
    time.sleep(1) # Send reply back to client 
    print(message)

0개의 댓글