Last active
August 29, 2015 14:13
-
-
Save htgoebel/c7bf6a19fef013d1f31b to your computer and use it in GitHub Desktop.
Minimal example for pyzmq, usable as test-case.
This file contains hidden or 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 threading | |
import uuid | |
import time | |
import zmq | |
def sender(send_addr, receiver_port): | |
send_socket = ctx.socket(zmq.REQ) | |
address = "tcp://{addr}:{port}".format(addr=send_addr, port=receiver_port) | |
send_socket.connect(address) | |
while True: | |
msg = uuid.uuid4() | |
print "sending: {}".format(msg) | |
send_socket.send_string(str(msg)) | |
if send_socket.recv() == "die": | |
return | |
time.sleep(3) | |
def receiver(receiver_port): | |
listener = ctx.socket(zmq.REP) | |
address = "tcp://*:{port}".format(port=receiver_port) | |
listener.bind(address) | |
while True: | |
msg = listener.recv_string() | |
listener.send("ack") | |
print "received: {}".format(msg) | |
if __name__ == "__main__": | |
ctx = zmq.Context() | |
port = 32429 | |
send = threading.Thread(target=sender, args=('localhost', port)) | |
send.start() | |
receive = threading.Thread(target=receiver, args=(port,)) | |
receive.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment