Created
October 25, 2011 13:36
-
-
Save jmoiron/1312743 to your computer and use it in GitHub Desktop.
eventlet + zmq + multiprocessing poc (similar to gevent_poc)
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Simple eventlet POC to check how to get ZMQ sockets working with | |
subprocesses spawned by a simple process.""" | |
import os | |
import eventlet | |
import multiprocessing | |
from eventlet.green import zmq | |
def log(msg): | |
print "(%s) %s" % (os.getpid(), msg) | |
def subprocess(ip, port): | |
c = zmq._Context() | |
log('Cid: %s' % id(c)) | |
socket = c.socket(zmq.REP) | |
socket.connect("tcp://%s:%s" % (ip, port)) | |
log("Connected to %s:%s" % (ip, port)) | |
msg = socket.recv() | |
log("Received msg: %s" % msg) | |
socket.send("Reply %s" % (os.getpid())) | |
eventlet.sleep(0) | |
c = zmq.Context() | |
log("Cid: %s" % id(c)) | |
socket = c.socket(zmq.REQ) | |
port = socket.bind_to_random_port('tcp://127.0.0.1') | |
# start subprocess | |
proc = multiprocessing.Process(target=subprocess, args=('127.0.0.1', port)) | |
proc2 = multiprocessing.Process(target=subprocess, args=('127.0.0.1', port)) | |
proc.start() | |
proc2.start() | |
log("Sending msg") | |
socket.send("Hello, world") | |
reply = socket.recv() | |
log("Reply received: %s" % reply) | |
# try it again; second worker should still be waiting on recv? | |
assert any([proc.is_alive(), proc2.is_alive()]) | |
socket.send("Hello, world") | |
reply = socket.recv() | |
log("Reply received: %s" % reply) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment