Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Created November 11, 2012 17:30
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 offlinehacker/4055607 to your computer and use it in GitHub Desktop.
Save offlinehacker/4055607 to your computer and use it in GitHub Desktop.
Python poll based multiprocessing example that works
import random
from multiprocessing import Pool,TimeoutError
from time import sleep
servers=["s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11","s12","s13","s14","s15","s16","s17","s18","s19","s20","s21","s22","s23","s24","s25","s26","s27","s28","s29","s30"]
blah = "no callback"
def f(x):
print("start f(" + x + ")")
sleep(random.randint(10, 20))
print("end f(" + x + ")")
return "did " + x
if __name__ == '__main__':
pool = Pool(processes=6)
results = pool.imap(f, servers, chunksize=1)
while True:
try:
print results.next()
except:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment