Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Pool, TimeoutError
- import time
- import os
- def f(x):
- print "ABBA"
- return x*x
- if __name__ == '__main__':
- pool = Pool(processes=4) # start 4 worker processes
- # print "[0, 1, 4,..., 81]"
- print pool.map(f, range(10))
- # print same numbers in arbitrary order
- for i in pool.imap_unordered(f, range(10)):
- print i
- # evaluate "f(20)" asynchronously
- res = pool.apply_async(f, (20,)) # runs in *only* one process
- print res.get(timeout=1) # prints "400"
- # evaluate "os.getpid()" asynchronously
- res = pool.apply_async(os.getpid, ()) # runs in *only* one process
- print res.get(timeout=1) # prints the PID of that process
- # launching multiple evaluations asynchronously *may* use more processes
- multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)]
- print [res.get(timeout=1) for res in multiple_results]
- # make a single worker sleep for 10 secs
- res = pool.apply_async(time.sleep, (10,))
- try:
- print res.get(timeout=1)
- except TimeoutError:
- print "We lacked patience and got a multiprocessing.TimeoutError"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement