[Python-ideas] Add create mode to open()

M.-A. Lemburg mal at egenix.com
Wed Aug 17 12:17:04 CEST 2011

Antoine Pitrou wrote:
> Le mercredi 17 août 2011 à 11:52 +0200, M.-A. Lemburg a écrit :
>> Antoine Pitrou wrote:
>>> On Wed, 17 Aug 2011 14:11:32 +1000
>>> Nick Coghlan <ncoghlan at gmail.com> wrote:
>>>> On Wed, Aug 17, 2011 at 1:43 PM, Devin Jeanpierre
>>>> <jeanpierreda at gmail.com> wrote:
>>>>> I'd also point out that for those that don't want race conditions,
>>>>> Python is discouraging. The correct incantation involves two
>>>>> undocumented constants, plus a unique and rarely used way of opening
>>>>> files that involves unix file descriptors.
>>>> FWIW, when you control the filename, you can include an additional
>>>> subdirectory precisely for the exception when a second process
>>>> attempts to create the same subdirectory.
>>> How do you create a directory and a file atomically?
>> On Windows, directories are created atomically. On Unix,
>> too, but symlinks are faster. You can use those to implement
>> cooperative file locks in a fairly cross-platform way.
> I was thinking of creating both the directory and the file in a single
> atomic operation. But if the directory is only ever used for that file,
> I guess it's ok.
> (there's still a problem when deleting the directory and the file, which
> can't be atomic, and the file has to be deleted before the directory,
> meaning if the process crashes in between, there are "legitimate"
> situations where the directory exists but not the file in it...)

The directory is only used as locking mechanism. You normally
don't need to create any files within that lock directory unless
you want to store extra lock information.
The lock directory can be create alongside the file you want
to lock or in a separate directory (on the same file system).

This mechanism can also be used to create directory/file
pairs - simply lock the directory (using a separate lock
directory), create the file, do something, remove the file,
remove the directory, remove lock.

If your process fails, you can use the information from the
lock directory to implement timeouts and cleanup actions (which
would then also remove the files in the directory you locked).

If you additionally add a lock info file to the lock directory,
you can make the checks even more sophisticated and check whether
the owning process still exists, the host owning the lock
is still available, etc.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Aug 17 2011)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
2011-10-04: PyCon DE 2011, Leipzig, Germany                48 days to go

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-ideas mailing list