Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Created on 23/04/2012
- @author: Davidmh
- '''
- from __future__ import division
- from multiprocessing import Queue, Process, Pool
- import time
- import random
- pars=[1,2]
- ncpu=2
- def calculate(par):
- time.sleep(0.5*random.random()+0.5)
- res=2*par
- print par,',',res
- q.put([par,res])
- print '---'
- def saving(q, savefile):
- print '==Saving=='
- while True:
- try:
- item=q.get(False)
- print item
- except :
- print '%'
- time.sleep(0.5)
- continue
- savefile.write(item[0])
- savefile.write(',')
- savefile.write(item[1])
- savefile.write('\r\n')
- if __name__=="__main__":
- print 'Started!'
- q=Queue()
- with open('test_par.txt', 'w', buffering=0) as savefile:
- savefile.write('This is the test file\r\n')
- print 'Standard map:'
- map(calculate, pars)
- print
- print 'Parallel map'
- pool = Pool(processes=min(ncpu, len(pars)))
- pool.map_async(calculate, pars, chunksize=1)
- pool.close()
- k=Process(target=saving, args=(q,savefile,))
- k.start()
- pool.join()
- print q.qsize()
- q.close()
- q.join_thread()
- k.terminate()
- pool.terminate()
- print 'End!'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement