Skip to main content

Improves fcntl.flock usage

Project description

https://img.shields.io/travis/AntoineCezar/flockcontext.svg https://img.shields.io/badge/docs-latest-brightgreen.svg https://img.shields.io/coveralls/AntoineCezar/flockcontext.svg https://img.shields.io/pypi/v/flockcontext.svg

Improves fcntl.flock usage.

flock is a Unix command for file locking, the mecanism that controls access restrictions of files.

Usage

Exclusive blocking lock:

from flockcontext import FlockOpen

with FlockOpen('/tmp/my.lock', 'w') as lock:
    lock.fd.write('Locked\n')

Exclusive blocking lock with 1 second timeout:

from flockcontext import FlockOpen

with FlockOpen('/tmp/my.lock', 'w', timeout=1) as lock:
    lock.fd.write('Locked\n')

Exclusive non-blocking lock:

from flockcontext import FlockOpen

try:
    with FlockOpen('/tmp/my.lock', 'w', blocking=False) as lock:
        lock.fd.write('Locked\n')
except IOError as e:
    print('Can not acquire lock')

Shared blocking lock:

from flockcontext import Flock

with FlockOpen('/tmp/my.lock', 'w', exclusive=False) as lock:
    lock.fd.write('Locked\n')

Acquire and release within context:

from flockcontext import FlockOpen

with FlockOpen('/tmp/my.lock', 'w') as lock:
    print('Lock acquired')
    lock.fd.write('Locked\n')

    lock.release()
    print('Lock released')

    lock.acquire()
    print('Lock acquired')
    lock.fd.write('Locked\n')

Locking already opened file:

from flockcontext import Flock

with open('/tmp/my.lock', 'w') as fd:
    with Flock(fd):
        fd.write('Locked\n')

License

  • Free software: BSD license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flockcontext-1.0.0.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flockcontext-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file flockcontext-1.0.0.tar.gz.

File metadata

  • Download URL: flockcontext-1.0.0.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for flockcontext-1.0.0.tar.gz
Algorithm Hash digest
SHA256 16c69ffdf483b5bef6cabf70b7fc830d5cf6e8e6708e61bd26a32738c683d523
MD5 fef3598ea49fefbf082cd47918e16f42
BLAKE2b-256 4dd41274ea691160ab4066e6e206f3d7ae245a81691b7f602f72ffb0d1f24d1f

See more details on using hashes here.

File details

Details for the file flockcontext-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flockcontext-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e38a0748fb4294591c3e20ae16ba9b4ec30f6e2f0494faf15aed28da4779ddf
MD5 cf9bd1b2c5dfa21aa62cee8a19814b7a
BLAKE2b-256 34810b88b8aafed29c50236d416af34bba6fd1760c3be801facc387b4b3cb2b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page