Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Queue, Process, Pool
- import random
- import time
- parameters=range(10)
- ncpu=2
- def calculate(par):
- # Do stuff
- time.sleep(5*random.random()) # Randomize writting times.
- savingfile.write(result)
- print result
- if __name__=="__main__":
- print 'Started!'
- with open('myfile.txt', 'w', buffering=0) as savefile:
- print 'Launched!'
- pool = Pool(processes=min(ncpu, len(parameters)))
- pool.map_async(calculate, parameters, chunksize=1)
- pool.close()
- pool.join()
- print 'End!'
Advertisement
Add Comment
Please, Sign In to add comment