-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPC using shared memory.py
More file actions
33 lines (30 loc) · 1.23 KB
/
IPC using shared memory.py
File metadata and controls
33 lines (30 loc) · 1.23 KB
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
import multiprocessing
import time
from multiprocessing import shared_memory
import numpy as np
def writer(shared_name, shape):
existing_shm = shared_memory.SharedMemory(name=shared_name)
shared_array = np.ndarray(shape, dtype=np.int64, buffer=existing_shm.buf)
for i in range(len(shared_array)):
shared_array[i] = i * 10
print("Writer has written data to the shared memory.")
existing_shm.close()
def reader(shared_name, shape):
time.sleep(1)
existing_shm = shared_memory.SharedMemory(name=shared_name)
shared_array = np.ndarray(shape, dtype=np.int64, buffer=existing_shm.buf)
print("Reader read data from the shared memory.", shared_array[:])
existing_shm.close()
existing_shm.unlink()
if __name__ == '__main__':
shape = (200,)
shm = shared_memory.SharedMemory(create=True, size=np.ndarray(shape, dtype=np.int64).nbytes)
shared_array = np.ndarray(shape, dtype=np.int64,buffer=shm.buf)
writer_process = multiprocessing.Process(target=writer, args=(shm.name, shape))
reader_process = multiprocessing.Process(target=reader, args=(shm.name, shape))
writer_process.start()
reader_process.start()
writer_process.join()
reader_process.join()
shm.close()
shm.unlink()