Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing.dummy import Pool as ThreadPool
- from time import sleep
- import random
- class bob:
- def __init__(self):
- self.res = []
- def do(self,x):
- l = [True,False]
- if random.choice(l):
- sleep(3)
- self.res.append(x)
- return 'bob'+str(x)
- urls = [
- 1,2,3,4,5,6,7,8,9,10
- ]
- for i in xrange(3):
- b = bob()
- # make the Pool of workers
- pool = ThreadPool(4)
- # open the urls in their own threads
- # and return the results
- pool.map(b.do, urls)
- # close the pool and wait for the work to finish
- pool.close()
- pool.join()
- print 'nn'
- for r in b.res:
- print r
Add Comment
Please, Sign In to add comment