Module filelock
source code
This module provides portable advisory file locking primitives that operate on
file descriptors. POSIX-like systems and Windows systems use different
primitives to perform file locking, and these different primitives are modeled
by incompatible (and different) modules in the Python standard library. This
module provides an abstract FileLock class, and underlying
implementations, to hide the operating system dependencies behind a simple
portable interface.
To create a file lock, simply instantiate the FileLock class with an open
file descriptor. It handles the rest:
from grizzled.io.filelock import FileLock
fd = open('/tmp/lockfile', 'r+')
lock = FileLock(fd)
lock.acquire()
...
lock.release()
You can also use the locked_file() function to simplify your code:
from grizzled.io.filelock import locked_file
fd = open('/tmp/lockfile', 'r+')
with locked_file(fd):
...
|
locked_file(*args,
**kwds)
This function is intended to be used as a with statement context
manager. |
source code
|
|
|
LOCK_CLASSES = { ' nt ' : ' _WindowsFileLock ' , ' posix ' : ' _PosixFile ...
|
|
__package__ = ' grizzled.io '
|
This function is intended to be used as a with statement context
manager. It wraps a FileLock object so that the locking and unlocking
of the file descriptor are automatic. With the locked_file() function,
you can replace this code:
lock = FileLock(fd)
lock.acquire()
try:
do_something()
finally:
lock.release()
with this code:
with locked_file(fd):
do_something()
- Parameters:
fd (int) - Open file descriptor. The file must be opened for writing
or updating, not reading.
no_wait (bool) - If False, then locked_file() will suspend the calling
process if someone has the file locked. If True, then
locked_file() will raise an IOError if the file is
locked by someone else.
- Decorators:
|
LOCK_CLASSES
- Value:
{ ' nt ' : ' _WindowsFileLock ' , ' posix ' : ' _PosixFileLock ' }
|
|