python zeromq control and pub(with poller) simple example

Hyunchul·2021년 9월 30일
0

Control


import zmq
import time


context = zmq.Context()
socket = context.socket(zmq.PUB) 
socket.bind("tcp://*:7000") 

time.sleep(2)

string = f"q,{time.time()}"
socket.send(string.encode())

Pub port listening control and send messages


import zmq
import time



context = zmq.Context()
pub = context.socket(zmq.PUB) 
pub.bind("tcp://*:7001") 

sub = context.socket(zmq.SUB) 
sub.connect("tcp://localhost:7000") 
t0 = time.time()
sub.subscribe("")

poller = zmq.Poller()
poller.register(sub, zmq.POLLIN)

while True:
    # listen
    socks = dict(poller.poll(1))
    if sub in socks:
        msg = sub.recv().decode()
        if 'q' in msg: break
    
    string = f"7001,{time.time()}"
    time.sleep(1/200)
    pub.send(string.encode())
    

0개의 댓글