I am using shared memory (sysv_ipc) between two different process and I want to see the last update time of the shared memory in another code. There are three programs, one writes to the shared memory, another reads from the shared memory, and the third one I need for external error handling, so I like to know if the shared memory is not updated for the last few minutes. With this idea, I tried accessing the attribute "last_attach_time" of the shared memory. It works fine when I ran it in the terminal. That is I created the object for shared memory once in the terminal and then I tried accessing the attribute continuously and it worked completely fine. Until the shared memory was written with data, the "last_attach_time" updated the time, and when writing stopped the output became constant and this is perfectly fine. But when I included in the external error handling code which has a while loop for continuous monitoring, the attribute is not giving correct data. ie, the time is still increasing even after writing to the shared memory is stopped. Has anyone faced similar issues? Thanks.
I'm the author of the Python sysv_ipc
module.
Without seeing your code, I can't say for sure what's happening. But I have a hunch.
In your monitor code, compare the memory segment's last_pid
value to the value of os.getpid()
. If it's the same, then there's your answer -- last_attach_time
is correctly reporting the time that your monitor program attached to the memory to see if anyone attached to it. :-)
Fuller explanation: Using a SysV IPC memory segment is a two-step process. First you create it, then you attach it. You can't do much with a memory segment that you haven't attached, so I wrote the sysv_ipc
module to automatically attach the segment for you in the Python constructor. In other words, the Python constructor does both steps (create and attach) for you. That's what it means when the documentation for the constructor says "The memory is automatically attached" (but that's easy to overlook).
So if your monitor code creates a new sysv_ipc.Semaphore()
object every time it runs, it will set last_attach_time
when it does so.
It sounds like you're more interested in the last write time which is not a value that SysV IPC provides. One approach would be to write a timestamp as part of the data you write to the shared memory.