Skip to main content

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.

Metadata

Release files for lock 2018.3.25.2110

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lock 2018.3.25.2110
File Size Uploaded
lock-2018.3.25.2110.tar.gz 3.0 kB Details

Release files / lock-2018.3.25.2110.tar.gz

Download URL lock-2018.3.25.2110.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cc5ac770930493eed7a8cfd0cf2568a125faf112eb8aa6b6149b3e581523d0c7
BLAKE2b-256 checksum
How to use checksums
eaee4c7e9740d397abd62b48bfe82f77f57a8984da4f5ba0a3b64930b44ccd8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

2018.3.25.2110 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page