Created
November 11, 2012 17:30
-
-
Save offlinehacker/4055607 to your computer and use it in GitHub Desktop.
Python poll based multiprocessing example that works
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
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