This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author nascheme
Recipients brett.cannon, davin, eric.snow, giampaolo.rodola, lukasz.langa, nascheme, osvenskan, pitrou, pmpp, rhettinger, ronaldoussoren, skrah, terry.reedy, yselivanov
Date 2019-02-17.21:35:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Some thoughts on this API.

I think we need the "create with exclusive behavior" option, even though we don't know how to implement it on Windows right now.  To me, there are two cases when calling SharedMemory:

1) You want to create a new shared memory object.  In this case, if your chosen name conflicts with an existing name, you should get an error.  If you don't, you could be sharing data between unrelated processes.  Chaos will ensue.

2) You want to attach to an existing shared memory object.  You have a name, passed to you by some means.  In this case, you don't want to create a new object if it doesn't exist.  You should get an error.

I thought there might be a 3rd case where you have "co-equal" processes and any one of them could create and the others would attach.  That dosen't seem safe though, if you can't ensure a unique name is used.  You might attach to some unrelated shared memory object due to a name conflict.  So, I think the API should not support this case.

To support 1 & 2, we could just have 'create'.  When true, it would act like O_CREX.  When false, you would get an error if the name doesn't already exist.

Regarding 'size', I think it is a bit weird how it currently works.  Maybe 'size' should only be valid if you are creating a new shared memory object.  If you are attaching to an existing one, size would be found from the existing object (if that's possible on all platforms).  SharedMemory could grow a "resize()" method that lets you enlarge the underlying memory object.  I don't know if that's useful in practice so maybe better to leave it out and keep the API simple.

Should 'size' be a property that always does fstat() to find the size of the underlying file?  Or, should it be the size passed in (for create=True) or the size of the file when the mmap is created.  I'm not sure but maybe it is better to not always do the fstat().

The 'read_only' keyword doesn't make sense if you care creating a new object and need to call ftruncate() on it.  I think the OS will not allow that.  Maybe 'read_only' should not be allowed if you are creating.  Or, you should have a method that takes a read-write object and makes it read-only.

On Linux at least, shm_open() is just a thin layer over open().  So the shared memory file is really just a regular file that is stored in /var/run/shm.  If you realize that, it seems SharedMemory() is much like tempfile.NamedTemporaryFile().  E.g. if you create a NamedTemporaryFile under /var/run/shm it will behave like the file descriptor created by SharedMemory().  SharedMemory() objects have the difference that they don't clean themselves up.  Also, they don't set the close-on-fork flag on the file descriptor.  Maybe they should?  It seems unclear to me how you should avoid cluttering /var/run/shm with shared memory objects that people forget to cleanup.  I guess the plan is that people need to call unlink() at the right time.  That seems error prone though.
Date User Action Args
2019-02-17 21:35:36naschemesetrecipients: + nascheme, brett.cannon, rhettinger, terry.reedy, ronaldoussoren, pitrou, osvenskan, giampaolo.rodola, skrah, pmpp, lukasz.langa, eric.snow, yselivanov, davin
2019-02-17 21:35:36naschemesetmessageid: <>
2019-02-17 21:35:36naschemelinkissue35813 messages
2019-02-17 21:35:36naschemecreate