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

History

0.4.0 (2019-07-03)

  • Use docker for mutiple Python version testing from 2.7 to 3.7

  • Add proper BSD licence file

  • Add timeout for blocking lock

  • Upgrade development status to Production/Stable

0.3.1 (2015-08-24)

  • Add syntax highlighting for code exemples

  • Add Flock manager exemple in README

0.3.0 (2015-08-21)

  • Add FlockOpen context manager.

0.2.0 (2015-08-20)

  • Add Flock relase and acquire capability withing context.

0.1.0 (2015-08-19)

  • Add Flock context manager.

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-0.4.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

flockcontext-0.4.0-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: flockcontext-0.4.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for flockcontext-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8bc2a410e12f5c5eafabe24404acb1a699f1b57cc1594ab343623a3f39d1b1d1
MD5 08d466b4f97b247cc2e370f60168a4c9
BLAKE2b-256 b6c35bd90034cc2634dbe58bd7c62941494a6538a1ab873ded646a9a0da281db

See more details on using hashes here.

File details

Details for the file flockcontext-0.4.0-py2.py3-none-any.whl.

File metadata

  • Download URL: flockcontext-0.4.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for flockcontext-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c1074a700cf6a3f18f95af3057acb757668bea31b539923a97c716a14ea197e1
MD5 0848cdd24d210b32934d14083d3b7fc3
BLAKE2b-256 eb33b5efd1199a729131f5569be6c774e1520b80857c937d99f813ff38d0b861

See more details on using hashes here.

Supported by

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