nextFileName(dirname,
ext,
directory=0)
Get a sequential file-name from a directory in a NFS and SMP-safe
way.
When you use this filename, do not run file() or symlink() on it; you
must create a file elsewhere (you can use a mutated version of the
returned filename to achieve SMP/NFS safety) and use os.rename over the
returned filename.
The returned filename includes the passed-in dirname; if you want an
absolute path, pass an absolute dirname - don't manipulate the returned
path.
This will allocate a file-name using the following strategy:
-
look for a symlink named ext.sequence
-
if it's found
-
read the body of this symlink and jump to that sequence number
-
otherwise
-
keep incrementing the sequence number until you arrive at a
[sequence].ext which does not exist. (this is verified by creating
a symlink, which is should always be an atomic test-and-set for
non-local filesystems.)
This algorithm is optimistic and may block for a while if there are a
lot of concurrent processes working on the same directory. Also, if you
delete a symlink created in this way, sequence numbers may repeat.
The optional 'directory' parameter will create a directory rather than
a symlink, which is also safe. As it is an empty directory, it will
support os.rename'ing another directory over it. At least on Linux. At
least on version 2.4. 10+. Maybe.
-
|