Advertisement
here2share

# ctype_multiprocessing.py

Dec 2nd, 2019
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. # ctype_multiprocessing.py
  2.  
  3. from multiprocessing import Process, Lock
  4. from multiprocessing.sharedctypes import Value, Array
  5. from ctypes import Structure, c_double
  6.  
  7. class Point(Structure):
  8.     _fields_ = [('x', c_double), ('y', c_double)]
  9.  
  10. def modify(n, d, s, A):
  11.     n.value **= 2
  12.     d.value **= 2
  13.     s.value = s.value.upper()
  14.     for a in A:
  15.         a.x *= 3
  16.         a.y *= 3
  17.  
  18. if __name__ == '__main__':
  19.     lock = Lock()
  20.  
  21.     n = Value('i', 7)
  22.     d = Value(c_double, 1.0/3.0, lock=False)
  23.     s = Array('c', b'hello world', lock=lock)
  24.     A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)
  25.  
  26.     print(n.value)
  27.     print(d.value)
  28.     print(s.value)
  29.     print([(a.x, a.y) for a in A])
  30.     print
  31.  
  32.     p = Process(target=modify, args=(n, d, s, A))
  33.     p.start()
  34.     p.join()
  35.  
  36.     print(n.value)
  37.     print(d.value)
  38.     print(s.value)
  39.     print([(a.x, a.y) for a in A])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement