Skip to content

Instantly share code, notes, and snippets.

@commandodev
Forked from taotetek/timer_test.py
Created January 29, 2011 17:53
Show Gist options
  • Save commandodev/802046 to your computer and use it in GitHub Desktop.
Save commandodev/802046 to your computer and use it in GitHub Desktop.
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