Obtain results from processes using python multiprocessing -
i trying understand how use multiprocessing module in python. code below spawns 4 processes , outputs results become available. seems me there must better way how results obtained queue; method not rely on counting how many items queue contains returns items become available , gracefully exits once queue empty. docs queue.empty() method not reliable. there better alternative how consume results queue?
import multiprocessing mp import time def multby4_wq(x, queue): print "starting!" time.sleep(5.0/x) = x*4 queue.put(a) if __name__ == '__main__': queue1 = mp.queue() in range(1, 5): p = mp.process(target=multbyc_wq, args=(i, queue1)) p.start() in range(1, 5): # referring counting again print queue1.get()
instead of using queue, how using pool?
for example,
import multiprocessing mp import time def multby4_wq(x): print "starting!" time.sleep(5.0/x) = x*4 return if __name__ == '__main__': pool = mp.pool(4) result in pool.map(multby4_wq, range(1, 5)): print result
pass multiple arguments
assume have function accept multiple parameters (add
in example). make wrapper function pass arguments add
(add_wrapper
).
import multiprocessing mp import time def add(x, y): time.sleep(1) return x + y def add_wrapper(args): return add(*args) if __name__ == '__main__': pool = mp.pool(4) result in pool.map(add_wrapper, [(1,2), (3,4), (5,6), (7,8)]): print result
Comments
Post a Comment