Portalocker

portalocker.lock(file_, flags)

Lock a file. Note that this is an advisory lock on Linux/Unix systems

portalocker.unlock(file_)

Unlock a file

portalocker.LOCK_EX = 2

Place an exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.

portalocker.LOCK_SH = 1

Place a shared lock. More than one process may hold a shared lock for a given file at a given time.

portalocker.LOCK_NB = 4

Acquire the lock in a non-blocking fashion.

portalocker.LOCK_UN = 8

Remove an existing lock held by this process.

exception portalocker.LockException

Bases: portalocker.exceptions.BaseLockException

Exception thrown if an error occurred during locking

portalocker.Lock

Locking utility class to automatically handle opening with timeouts and context wrappers

exception portalocker.AlreadyLocked

Bases: portalocker.exceptions.BaseLockException

Exception thrown when the file is already locked by someone else

portalocker.open_atomic(*args, **kwds)

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)

Tests

Table Of Contents

Related Topics

This Page