Skip to main content

Improves fcntl.flock usage

Project description

Improves fcntl.flock usage.

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


Exclusive blocking lock:

from flockcontext import FlockOpen

with FlockOpen('/tmp/my.lock', 'w') as lock:

Exclusive blocking lock with 1 second timeout:

from flockcontext import FlockOpen

with FlockOpen('/tmp/my.lock', 'w', timeout=1) as lock:

Exclusive non-blocking lock:

from flockcontext import FlockOpen

    with FlockOpen('/tmp/my.lock', 'w', blocking=False) as lock:
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:

Acquire and release within context:

from flockcontext import FlockOpen

with FlockOpen('/tmp/my.lock', 'w') as lock:
    print('Lock acquired')

    print('Lock released')

    print('Lock acquired')

Locking already opened file:

from flockcontext import Flock

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


  • Free software: BSD license


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.

Files for flockcontext, version 0.4.0
Filename, size File type Python version Upload date Hashes
Filename, size flockcontext-0.4.0-py2.py3-none-any.whl (5.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size flockcontext-0.4.0.tar.gz (14.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page