Skip to content

Instantly share code, notes, and snippets.

@ionelmc
Created May 3, 2013 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ionelmc/5509195 to your computer and use it in GitHub Desktop.
Save ionelmc/5509195 to your computer and use it in GitHub Desktop.
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