Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # generic_multiprocessing.py
- from multiprocessing.pool import ThreadPool
- import multiprocessing
- import time
- p=10
- def worker(x):
- time.sleep(1) # to similate delayed processing of variable p seconds
- s = "[%s: time = %s]" % (x+1, ms())
- print s; return s # note: to print while multiprocessing may appear conficting in itself
- #
- def start(): return time.time()
- def ms(): return int((time.time()-st)*100)/100.00
- print "test without multiprocessing --" # waaaaait for it...
- st = start()
- for i in range(p):
- worker(i)
- print
- print ms(),
- print "seconds\n\n\ntest with the multiprocessing --"
- def group(i):
- w = worker(i)
- return w
- st = start()
- # CPUs = multiprocessing.cpu_count() ###??? far slower than ThreadPool(128)
- pool = ThreadPool(128)
- results = []
- results.append(pool.map(group, range(p)))
- print results[0] # automatically sorted? haha... yeahhhhh!
- print
- print ms(), 'seconds'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement