-
-
Save winterstream/3520624 to your computer and use it in GitHub Desktop.
fork a process in gevent for cpu intensive stuff / with zeromq communication
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
# You need gevent 1.0 and pyzmq 3.x currently unreleased | |
# | |
# pip install hg+https://bitbucket.org/denis/gevent | |
# pip install git+git://github.com/zeromq/pyzmq.git | |
# | |
import gevent | |
import zmq.green as zmq | |
import os, sys | |
ADDR = 'tcp://127.0.0.1:5555' | |
ctx = zmq.Context() | |
def run_parent(): | |
sock = ctx.socket(zmq.PUSH) | |
sock.bind(ADDR) | |
for i in range(10): | |
sock.send('message: %d' % i) | |
gevent.sleep(1) | |
def run_child(ident): | |
# create a new context since we are forked in a new process | |
ctx = zmq.Context() | |
sock = ctx.socket(zmq.PULL) | |
sock.connect(ADDR) | |
while True: | |
msg = sock.recv() | |
print '%s: %s' % (ident, msg) | |
def fork_workers(num): | |
pids = [] | |
for i in range(num): | |
pid = gevent.fork() | |
if pid == 0: | |
run_child(os.getpid()) | |
sys.exit(0) | |
else: | |
pids.append(pid) | |
return pids | |
pids = fork_workers(3) | |
print 'workers:', ', '.join('%d' % p for p in pids) | |
run_parent() | |
# not cool, workers should die themselves actually | |
for pid in pids: | |
os.kill(pid, 15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment