portalocker.utils module

class portalocker.utils.Lock(filename, mode='a', timeout=5, check_interval=0.25, fail_when_locked=False, flags=6)[source]

Bases: object

acquire(timeout=None, check_interval=None, fail_when_locked=None)[source]

Acquire the locked filehandle

release()[source]

Releases the currently locked file handle

portalocker.utils.open_atomic(*args, **kwds)[source]

Open a file for atomic writing. Instead of locking this method allows you to write the entire file and move it to the actual location. Note that this makes the assumption that a rename is atomic on your platform which is generally the case but not a guarantee.

http://docs.python.org/library/os.html#os.rename

>>> filename = 'test_file.txt'
>>> if os.path.exists(filename):
...     os.remove(filename)
>>> with open_atomic(filename) as fh:
...     written = fh.write(b'test')
>>> assert os.path.exists(filename)
>>> os.remove(filename)