Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Created October 25, 2011 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmoiron/1312333 to your computer and use it in GitHub Desktop.
Save jmoiron/1312333 to your computer and use it in GitHub Desktop.
gevent + zmq + multiprocessing poc
#!/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