Shared memory between processes
import logging
import multiprocessing
import time
logging.basicConfig(level=logging.DEBUG, format='%(processName)s: %(message)s')
def f(num, arr):
logging.debug(num)
num.value += 1.0
logging.debug(arr)
for i in range(len(arr)):
arr[i] *= 2
if __name__ == '__main__':
num = multiprocessing.Value('f', 0.0)
arr = multiprocessing.Array('i', [1, 2, 3, 4, 5])
p1 = multiprocessing.Process(target=f, args=(num, arr))
p2 = multiprocessing.Process(target=f, args=(num, arr))
p1.start()
p2.start()
p1.join()
p2.join()
logging.debug(num.value)
logging.debug(arr[:])
Process-1: <Synchronized wrapper for c_float(0.0)>
Process-1: <SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_int_Array_5 object at 0x1113b73b0>>
Process-2: <Synchronized wrapper for c_float(1.0)>
Process-2: <SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_int_Array_5 object at 0x1113b73b0>>
MainProcess: 2.0
MainProcess: [4, 8, 12, 16, 20]