Created
May 3, 2013 13:46
-
-
Save ionelmc/5509195 to your computer and use it in GitHub Desktop.
This file contains 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 os | |
import time | |
import itertools | |
import socket | |
import tempfile | |
name = tempfile.mktemp('.sock') | |
sr, ss = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM) | |
pids = [] | |
for i in range(4): | |
pid = os.fork() | |
if pid: | |
print "Forked:", pid | |
pids.append(pid) | |
else: | |
break | |
if pid: | |
print "Producing ..." | |
for i in range(100): | |
ss.send("item-%s" % i) | |
for i in range(4): | |
ss.send('sentinel') | |
print "Produced. Waiting for workers now ..." | |
for pid in pids: | |
os.waitpid(pid, 0) | |
else: | |
mypid = os.getpid() | |
counter = 0 | |
while 1: | |
if sr.recvfrom(1024)[0] == 'sentinel': | |
break | |
counter += 1 | |
time.sleep(0.05) # simulate work | |
print mypid, '->', counter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment