Python Shared Memory Multiprocessing - running functions on data write

Viewed 67

I am using shared_memory from multiprocessing between different languages (Julia and python). I want to run a function when the memory is written to (the python should only really read from the memory but i use the first 4 bytes as a flag to allow Julia to write to the shared memory). I can't find a good way to detect the change except loop (which adds delay between the shared memory). Is there a better way?

from multiprocessing import shared_memory
import array
import time
import GSTiming

def main():

    shm_a = shared_memory.SharedMemory("myid", create=True, size=512)
    shm_a.buf[0] = 1 # Modify single byte at a time
    u = []
    for i in range(105):
        while shm_a.buf[0] == 1:
            GSTiming.delayMicroseconds(1)
        a = GSTiming.micros()
        s = bytes(shm_a.buf[4:shm_a.buf[0]+4])
        shm_a.buf[0] = 1
        b = GSTiming.micros()
        u.append(b-a)
    print(u)
    shm_a.close()
    shm_a.unlink()  # Call unlink only once to release the shared memory

main()
0 Answers
Related