You can use Python's tempfile
module to give you a temporary file name. It can create a temporary file in a thread safe manner rather than making one up using time.time()
which may return the same name if used in multiple threads at the same time.
As suggested in a comment to your question, this can be coupled with the use of a context manager. You can get some ideas of how to implement what you want to do by looking at Python tempfile.py
sources.
The following code snippet may do what you want. It uses some of the internals of the objects returned from tempfile
.
- Creation of temporary files is thread safe.
- Renaming of files upon successful completion is atomic, at least on Linux. There isn't a separate check between
os.path.exists()
and the os.rename()
which could introduce a race condition. For an atomic rename on Linux the source and destinations must be on the same file system which is why this code places the temporary file in the same directory as the destination file.
- The
RenamedTemporaryFile
class should behave like a NamedTemporaryFile
for most purposes except when it is closed using the context manager, the file is renamed.
Sample:
import tempfile
import os
class RenamedTemporaryFile(object):
"""
A temporary file object which will be renamed to the specified
path on exit.
"""
def __init__(self, final_path, **kwargs):
tmpfile_dir = kwargs.pop('dir', None)
# Put temporary file in the same directory as the location for the
# final file so that an atomic move into place can occur.
if tmpfile_dir is None:
tmpfile_dir = os.path.dirname(final_path)
self.tmpfile = tempfile.NamedTemporaryFile(dir=tmpfile_dir, **kwargs)
self.final_path = final_path
def __getattr__(self, attr):
"""
Delegate attribute access to the underlying temporary file object.
"""
return getattr(self.tmpfile, attr)
def __enter__(self):
self.tmpfile.__enter__()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type is None:
self.tmpfile.delete = False
result = self.tmpfile.__exit__(exc_type, exc_val, exc_tb)
os.rename(self.tmpfile.name, self.final_path)
else:
result = self.tmpfile.__exit__(exc_type, exc_val, exc_tb)
return result
You can then use it like this:
with RenamedTemporaryFile('whatever') as f:
f.write('stuff')
During writing, the contents go to a temporary file, on exit the file is renamed. This code will probably need some tweaks but the general idea should help you get started.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…