module for enabling file locks
Project description
lock
This is a module for enabling file locks. It uses fcntl to accomplish this.
POSIX operating systems implement advisory file locking. This enables concurrent processes to interact with the same file without conflict, provided they first check for the existence of a lock held by a different process. Mandatory file locking can be enabled on POSIX systems by mounting volumes using the mount -o mand option and makes the operating system prevent editing of a locked file.
The fcntl functionality is implemented in Python by the fcntl standard library module. Using this, a lock can be acquired in a way like the following:
lock_file = open("data.db", "a")
fcntl.lockf(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
If the file is locked, the following exception is raised:
BlockingIOError: [Errno 11] Resource temporarily unavailable
With advisory locking, code should test for the existence of a a lock before editing a file. It is the responsibility of the code, not the operating system, to enforce locking correctly. Information about the locks currently held on a file may be retrieved by passing a bytes object as the third argument to the fcntl.fcntl function (which takes the place of a pointer to the process ID in the fcntl header). The lock information can be accessed in a way like the following:
lock_data = struct.pack("hhllhh", fcntl.F_WRLCK, 0, 0, 0, 0, 0)
fcntl.fcntl(lock_file, fcntl.F_GETLK, lock_data)
This returns lock_data unchanged if there is no lock on the file.
examples
The following code saves a dictionary to a file in a loop using lock to lock the file while the save is happening:
import random
import lock
while True:
config = {"a": 1, "b": random.randint(1, 2)}
lock.save_JSON("config.json", config)
The following code loads a dictionary from a file in a loop using lock to lock the file while the load is happening:
import lock
while True:
config = lock.load_JSON("config.json")
if config: print(config)
These two pieces of code can be used concurrently.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file lock-2018.3.25.2110.tar.gz
.
File metadata
- Download URL: lock-2018.3.25.2110.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc5ac770930493eed7a8cfd0cf2568a125faf112eb8aa6b6149b3e581523d0c7 |
|
MD5 | 435db9197f018956cbdddb58af977858 |
|
BLAKE2b-256 | eaee4c7e9740d397abd62b48bfe82f77f57a8984da4f5ba0a3b64930b44ccd8f |