Original link: https://chegva.com/5605.html
◎Knowledge points
-
Shared memory for interprocess communication
-
Insecurity of shared data in multi-process operation
-
The insecurity of sharing data in multi-threaded operations
◎Script practice
▽ Shared memory for interprocess communication
""" If you want to implement communication between processes, shared memory is one of the common implementations. It allows multiple processes to directly access the same block of memory. The type of the object in shared memory must be ctypes. ctypes is a data type compatible with the C language. To create objects of type ctypes in shared memory, the standard library module multiprocessing provides the following two functions: (1) Value(typecode_or_type: Any, *args: Any, lock: bool = ...) -> sharedctypes._Value: ... The return value represents a numeric value. The parameter typecode_or_type is used to specify the type code or ctypes type of the value. (2) def Array(typecode_or_type: Any, size_or_initializer: Union[int, Sequence[Any]], *, lock: bool = ...) The return value represents an array. The parameter typecode_or_type is used to specify the typecode or ctypes type of the elements in the array. The parameter size_or_initializer is used to specify the length of the array or sequence in python. """ from multiprocessing import Process, Value, Array import ctypes def do_sth0(number, array): number.value = 1.8 for i in range(len(array)): array[i] = -array[i] if __name__ == '__main__': # Create a ctypes object representing a value in shared memory number = Value('d', 2.3) # <Synchronized wrapper for c_double(2.3)> array = Array('i', range(1, 5)) # <SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_int_Array_4 object at 0x10f43f3c0>> p = Process(target=do_sth0, args=(number, array)) p. start() p. join() print(number. value) # 1.8 print(array[:]) # [-1, -2, -3, -4]
▽ Insecurity of shared data in multi-process operation
""" Since the execution of multi-process is uncertain, the result of multi-process operation sharing data is unpredictable, which is often called unsafe. """ from multiprocessing import Process, Value, Array def do_sth(num): for i in range(100): # Equivalent to: num.value = num.value + 1 # First calculate num.value + 1, store it in a temporary variable, and then assign the value of the temporary variable to num num.value += 1 def do_sth1(arr): for i in range(len(arr)): arr[i] = -arr[i] if __name__ == '__main__': num = Value('i', 0) arr = Array('i', range(10)) p1 = Process(target=do_sth, args=(num,)) p2 = Process(target=do_sth, args=(num,)) p3 = Process(target=do_sth1, args=(arr,)) p1. start() p2. start() p3. start() p1. join() p2. join() p3. join() print(num. value) # not sure print(arr[:]) # [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
▽ The insecurity of sharing data in multi-threaded operations
""" Since the execution of multithreading is uncertain, the result of multithreading operation sharing data is unpredictable, which is often called unsafe. """ from threading import Thread num2 = 0 def do_sth2(): global num2 for i in range(100000): num2 += 1 if __name__ == '__main__': t1 = Thread(target=do_sth2) t2 = Thread(target=do_sth2) t1. start() t2. start() t1. join() t2. join() print(num2) # not sure
◎Script address: https://github.com/anzhihe/learning/blob/master/python/practise/learn-python/python_senior/process(thread)_sharedata.py
This article is transferred from: https://chegva.com/5605.html
This site is only for collection, and the copyright belongs to the original author.