Guest User

Untitled

a guest
Nov 18th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. from multiprocessing import Manager,Pool
  2.  
  3. def modify_dictionary(dictionary):
  4. if((3,3) not in dictionary):
  5. dictionary[(3,3)]=0.
  6. for i in range(100):
  7. dictionary[(3,3)] = dictionary[(3,3)]+1
  8. return 0
  9.  
  10. if __name__ == "__main__":
  11.  
  12. manager = Manager()
  13.  
  14. dictionary = manager.dict(lock=True)
  15. jobargs = [(dictionary) for i in range(5)]
  16.  
  17. p = Pool(5)
  18. t = p.map(modify_dictionary,jobargs)
  19. p.close()
  20. p.join()
  21.  
  22. print dictionary[(3,3)]
  23.  
  24. dictionary[(3,3)] = dictionary[(3,3)]+1
  25.  
  26. from multiprocessing import Manager,Pool,Lock
  27.  
  28. lock = Lock()
  29.  
  30. def modify_array(dictionary):
  31. if((3,3) not in dictionary):
  32. dictionary[(3,3)]=0.
  33. for i in range(100):
  34. with lock:
  35. dictionary[(3,3)] = dictionary[(3,3)]+1
  36. return 0
  37.  
  38. if __name__ == "__main__":
  39.  
  40. manager = Manager()
  41.  
  42. dictionary = manager.dict(lock=True)
  43. jobargs = [(dictionary) for i in range(5)]
  44.  
  45. p = Pool(5)
  46. t = p.map(modify_array,jobargs)
  47. p.close()
  48. p.join()
  49.  
  50. print dictionary[(3,3)]
  51.  
  52. from multiprocessing import Process, Manager, Pool, Lock
  53. from functools import partial
  54.  
  55. def f(dictionary, l, k):
  56. with l:
  57. for i in range(100):
  58. dictionary[3] += 1
  59.  
  60. if __name__ == "__main__":
  61. manager = Manager()
  62. dictionary = manager.dict()
  63. lock = manager.Lock()
  64.  
  65. dictionary[3] = 0
  66. jobargs = list(range(5))
  67. pool = Pool()
  68. func = partial(f, dictionary, lock)
  69. t = pool.map(func, jobargs)
  70.  
  71. pool.close()
  72. pool.join()
  73.  
  74. print(dictionary)
Add Comment
Please, Sign In to add comment