Skip to content

Instantly share code, notes, and snippets.

@graingert
Created December 4, 2012 20:40
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 graingert/4208460 to your computer and use it in GitHub Desktop.
Save graingert/4208460 to your computer and use it in GitHub Desktop.
from multiprocessing import Pool, cpu_count
import time
from random import randint
def roll(dice=1, sides=6):
try:
return [randint(1, sides) for i in range(dice)]
except:
return []
def f(x):
time.sleep(roll(1, 6)[0])
return x
if __name__ == '__main__':
p = Pool(cpu_count())
for item in p.imap(f, range(100)):
print item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment