Problem:
Currently, let’s say I create a shared_memory segment using
mulitprocessing.shared_memory.SharedMemory
<https://docs.python.org/3.10/library/multiprocessing.shared_memory.html> in
Process 1 and open the same in Process 2.
Then, I try to write some data to the shared memory segment using both the
processes, so for me to prevent any race condition (data corruption), either
these operations must be atomic, or I should be able to lock / unlock shared
memory segment, which I cannot at the moment.
I earlier posted a solution
<https://mail.python.org/archives/list/python-ideas@python.org/thread/X4AKFFMYEKW6GFOUMXMOJ2OBINNY2Q6L/>
to this problem, which received positive response, but there weren’t many
responses to it, despite the fact this problem makes shared_memory practically
unusable if there are simultaneous writes.
So, the purpose of this post is to have discussion about the solution of the
same.
Some Solutions:
1. Support for shared semaphores across unrelated processes to lock/unlock
shared memory segment.
--> More Details
<https://mail.python.org/archives/list/python-ideas@python.org/thread/X4AKFFMYEKW6GFOUMXMOJ2OBINNY2Q6L/>
2. Let the first bit in the shared memory segment be the synchronisation bit
used for locking/unlocking.
--> A process can only lock the shared memory segment if this bit is unset,
and will set this bit after acquiring lock, and similarly unset this bit
after unlocking. Although set/unset operations must be atomic. Therefore, the
following tools can be used:
type __sync_add_and_fetch (type *ptr, type value, ...)
type __sync_sub_and_fetch (type *ptr, type value, ...)
type __sync_or_and_fetch (type *ptr, type value, ...)
Documentation of these can be found at below links:
https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins
<https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html#g_t_005f_005fsync-Builtins>
https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins
<https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html#g_t_005f_005fatomic-Builtins>
Any other ideas/solutions are very welcome.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/7C3TART5SPHLJZZHZIY43KUEIB2IUX75/
Code of Conduct: http://python.org/psf/codeofconduct/