-
-
Save commandodev/802046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import eventlet | |
from datetime import datetime | |
from eventlet.green import zmq | |
from eventlet.hubs import use_hub | |
use_hub('zeromq') | |
def transmit(): | |
context = zmq.Context() | |
publisher = context.socket(zmq.PUB) | |
publisher.bind("tcp://*:5558") | |
eventlet.sleep(0.1) | |
counter = 0 | |
while counter < 10: | |
publisher.send("PULSE") | |
print("PULSE snt at %s" % datetime.now()) | |
counter += 1 | |
eventlet.sleep() | |
publisher.send("TERM") | |
print("TERM snt at %s" % datetime.now()) | |
def listener(pnum): | |
context = zmq.Context() | |
pulse_receiver = context.socket(zmq.SUB) | |
pulse_receiver.connect("tcp://*:5558") | |
pulse_receiver.setsockopt(zmq.SUBSCRIBE,"") | |
while True: | |
message = pulse_receiver.recv() | |
if message == "PULSE": | |
print("PULSE rcv at %s by %i" % (datetime.now(), pnum)) | |
elif message == "TERM": | |
print("TERM rcv at %s by %i" % (datetime.now(), pnum)) | |
break | |
if __name__ == "__main__": | |
listeners = 10 | |
for pnum in range(listeners): | |
eventlet.spawn_n(listener, pnum) | |
transmit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment