Guest User

Untitled

a guest
Jul 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. from multiprocessing.dummy import Pool as ThreadPool
  2. from time import sleep
  3. import random
  4.  
  5. class bob:
  6. def __init__(self):
  7. self.res = []
  8. def do(self,x):
  9. l = [True,False]
  10. if random.choice(l):
  11. sleep(3)
  12. self.res.append(x)
  13. return 'bob'+str(x)
  14.  
  15.  
  16. urls = [
  17. 1,2,3,4,5,6,7,8,9,10
  18. ]
  19.  
  20.  
  21. for i in xrange(3):
  22. b = bob()
  23. # make the Pool of workers
  24. pool = ThreadPool(4)
  25.  
  26. # open the urls in their own threads
  27. # and return the results
  28. pool.map(b.do, urls)
  29.  
  30. # close the pool and wait for the work to finish
  31. pool.close()
  32. pool.join()
  33. print 'nn'
  34. for r in b.res:
  35. print r
Add Comment
Please, Sign In to add comment