Dapid

Untitled

May 6th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from multiprocessing import Queue, Process, Pool
  2. import random
  3. import time
  4.  
  5. parameters=range(10)
  6. ncpu=2
  7.  
  8. def calculate(par):
  9.         # Do stuff
  10.     time.sleep(5*random.random()) # Randomize writting times.
  11.     savingfile.write(result)
  12.     print result
  13.            
  14. if __name__=="__main__":
  15.     print 'Started!'
  16.    
  17.     with open('myfile.txt', 'w', buffering=0) as savefile:
  18.         print 'Launched!'
  19.  
  20.         pool = Pool(processes=min(ncpu, len(parameters)))
  21.         pool.map_async(calculate, parameters, chunksize=1)
  22.         pool.close()
  23.         pool.join()
  24.    
  25.     print 'End!'
Advertisement
Add Comment
Please, Sign In to add comment