Single Instance of app

Tim Peters tim.one at home.com
Wed Aug 29 01:00:35 EDT 2001


[Tim Peters]
> I'm not sure exactly what's meant by "atomic lock on a file",
> but you can study the std tempfile.py module for the excruciating dance
> needed to create temp files safely.

[Tim Hammerquist]
> Thanks!

Hey, what else are Tims good for <wink>?

> I just meant 'atomic' as far as avoiding the race condition.

Cool.  You have primarily the O_EXCL flag to os.open() to thank for that.

> The tempfile module uses the ((fd)?open|close) functions in the os
> module. I would assume these are closer to the system calls like
> fopen() and fdopen() than is the built-in open()?

           Python   C
-----------------   -------
 __builtin__.open   fopen
__builtin__.close   fclose
          os.open   open
         os.close   close
        os.fdopen   fdopen

The problem is that O_EXCL isn't available to (C) fopen, so the low-level
open has to be used.  Then the file is unlinked, both so that it will go
away when the descriptor is closed, and so that nobody can find it *by* name
via directory searches.  Then we're left with a raw file handle, but Python
file objects wrap streams; fdopen is used just to wrap a stream around the
handle.





More information about the Python-list mailing list