-
-
Save tmc/1313179 to your computer and use it in GitHub Desktop.
gevent + zmq + multiprocessing 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 gevent POC to check how to get ZMQ sockets working with | |
subprocesses spawned by a simple process.""" | |
# this code doesn't seem to work properly; it has a | |
# tendency to hang at various parts of the req/rep process | |
# all of this stuff works flawlessly if you change: | |
import gevent | |
from gevent_zeromq import zmq | |
# to: | |
# import time as gevent | |
# import zmq | |
import os | |
import multiprocessing | |
def subprocess(ip, port): | |
c = zmq.Context() | |
print 'pid: %s, cid: %s' % (os.getpid(), id(c)) | |
socket = c.socket(zmq.REP) | |
socket.connect("tcp://%s:%s" % (ip, port)) | |
print "connected to %s:%s" % (ip, port) | |
msg = socket.recv() | |
print "Received msg: %s" % msg | |
print "Sending reply: \"Hello, ZMQ\"" | |
socket.send("Hello, ZMQ") | |
gevent.sleep(0.5) | |
# c.term() should hang here til the socket sends but hangs forever instead | |
c = zmq.Context() | |
print 'pid: %s, cid: %s' % (os.getpid(), 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)) | |
proc.start() | |
gevent.sleep(0.2) | |
print "Sending msg: \"Hello, World\"" | |
socket.send("Hello, world") | |
print "Waiting for reply" | |
reply = socket.recv() | |
print "Received reply: %s" % reply |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment