Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

MPAtomicInt32.py 1.9 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  1. import multiprocessing
  2. class MPAtomicInt32:
  3. """
  4. Multiprocess atomic int32 variable
  5. using multiprocessing.RawArray at specified index
  6. """
  7. def __init__(self, ar : multiprocessing.RawArray=None, index=None):
  8. if ar is None:
  9. ar = multiprocessing.RawArray('B', 4)
  10. self._ar = ar
  11. if index is None:
  12. index = 0
  13. self._index = index
  14. self._mv = memoryview(ar).cast('B')[index:index+4].cast('i')
  15. self._mv[0] = 0
  16. self._lock = multiprocessing.Lock()
  17. def compare_exchange(self, cmp_val, new_val):
  18. mv = self._mv
  19. initial_val = mv[0]
  20. if initial_val == cmp_val:
  21. self._lock.acquire()
  22. initial_val = mv[0]
  23. if initial_val == cmp_val:
  24. mv[0] = new_val
  25. self._lock.release()
  26. return initial_val
  27. def multi_compare_exchange(self, val_or_list, new_val):
  28. if not isinstance(val_or_list, (tuple,list)):
  29. val_or_list = (val_or_list,)
  30. mv = self._mv
  31. initial_val = mv[0]
  32. if any( initial_val == val for val in val_or_list ):
  33. self._lock.acquire()
  34. initial_val = mv[0]
  35. if any( initial_val == val for val in val_or_list ):
  36. mv[0] = new_val
  37. self._lock.release()
  38. return initial_val
  39. def get(self):
  40. return self._mv[0]
  41. def set(self, new_val, with_lock=True):
  42. if with_lock:
  43. self._lock.acquire()
  44. self._mv[0] = new_val
  45. if with_lock:
  46. self._lock.release()
  47. def __getstate__(self):
  48. d = self.__dict__.copy()
  49. # pop unpicklable memoryview object
  50. d.pop('_mv')
  51. return d
  52. def __setstate__(self, d):
  53. # restore memoryview of RawArray
  54. self.__dict__.update(d)
  55. self._mv = memoryview(self._ar).cast('B')[self._index:self._index+4].cast('i')
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...