Quick Start Guide
This is the Quick Start guide for your project. In this page we will the discuss the use of share memory mainly via the python interface for examples. The theory and stratergies can be used in C/C++ as well.
Reading
To open an ecisting shared memory file and read the data use the following code:
import dao
shm = dao.shm('file.im.shm')
data = shm.get_data()
Here a numpy data array is not passed as the data type and shape are read from the shared memory file. We use get_data() to read the data without checking any semaphores.
Writing
import dao
shm = dao.shm('file.im.shm')
data = np.zeros([10,10]).astype(np.uint16)
data = shm.set_data(data) # write data to shared memory
Syncronisation
data = shm.get_data(check=True)
This will check the semaphore before reading the data. If the semaphore is locked it will block until it is unlocked before reading the data. This is useful when reading data that is being written to by another process.
Note
If shared memory has just been created then the semaphore will release immediately as it counts as the first write.
If multiple processes are reading from the same shared memory file then they can all read at the same time. This is due to multiple semaphores attached to each shared memory file, by default 10 semaphores are created.
To specify the number of semaphores to use when reading use:
import dao
shm = dao.shm('file.im.shm')
data = shm.get_data(check=True, nbSem=5)
The other way to use the semaphore is to spin on a counter in the SHM file to change. This forces the code to stay in usersapace and block until the shm is updated. This is very useful in real-time processes as the function returns quicker then using the semaphore but it adds complexity to the system. This is really only useful for C/C++ tasks that need to run at high speed.
data = shm.get_data(spin=True)
Simple Loop
Writer example
import dao
import numpy as np
data = np.zeros([10,10]).astype(np.uint16)
shm = dao.shm('/tmp/file.im.shm', data)
while True:
data += 1
data = shm.set_data(data)
time.sleep(0.1)
Reader example
import dao
shm = dao.shm('/tmp/file.im.shm')
while True:
data = shm.get_data(check=True)
print(data)