Note: This module will become obsolete in a future release. The locking operation that it provides is done better and more portably by the fcntl.lockf() call.
This module implements some additional functionality over the built-in file objects. In particular, it implements file locking, control over the file flags, and an easy interface to duplicate the file object. The module defines a new file object, the posixfile object. It has all the standard file object methods and adds the methods described below. This module only works for certain flavors of Unix, since it uses fcntl.fcntl() for file locking.
To instantiate a posixfile object, use the open() function in the posixfile module. The resulting object looks and feels roughly the same as a standard file object.
The posixfile module defines the following constants:
The posixfile module defines the following functions:
The posixfile object defines the following additional methods:
All methods raise IOError when the request fails.
Format characters for the lock() method have the following meaning:
Format | Meaning |
---|---|
u | unlock the specified region |
r | request a read lock for the specified section |
w | request a write lock for the specified section |
In addition the following modifiers can be added to the format:
Modifier | Meaning | Notes |
---|---|---|
| | wait until the lock has been granted | |
? | return the first lock conflicting with the requested lock, or None if there is no conflict. | (1) |
Note:
(1) The lock returned is in the format (mode, len, start, whence, pid) where mode is a character representing the type of lock ('r' or 'w'). This modifier prevents a request from being granted; it is for query purposes only.
Format characters for the flags() method have the following meanings:
Format | Meaning |
---|---|
a | append only flag |
c | close on exec flag |
n | no delay flag (also called non-blocking flag) |
s | synchronization flag |
In addition the following modifiers can be added to the format:
Modifier | Meaning | Notes |
---|---|---|
! | turn the specified flags 'off', instead of the default 'on' | (1) |
= | replace the flags, instead of the default 'OR' operation | (1) |
? | return a string in which the characters represent the flags that are set. | (2) |
Note:
(1) The "!" and "=" modifiers are mutually exclusive.
(2) This string represents the flags after they may have been altered by the same call.
Examples:
import posixfile file = posixfile.open('/tmp/test', 'w') file.lock('w|') ... file.lock('u') file.close()
guido@python.org