Metadata-Version: 2.3
Name: flockcontext
Version: 1.0.0
Summary: Improves fcntl.flock usage
Author: Antoine Cezar
Author-email: Antoine Cezar <AntoineCezar@users.noreply.github.com>
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.10
Requires-Dist: timeoutcontext>=2.0.0
Requires-Python: >=3.10
Project-URL: Changelog, https://github.com/me/spam/blob/master/HISTORY.rst
Project-URL: Documentation, http://flockcontext.readthedocs.org/
Project-URL: Homepage, https://github.com/AntoineCezar/flockcontext
Project-URL: Issues, https://github.com/AntoineCezar/flockcontext/issues
Project-URL: Repository, http://flockcontext.readthedocs.org/
Description-Content-Type: text/x-rst

===============================
Flockcontext
===============================

.. image:: https://img.shields.io/travis/AntoineCezar/flockcontext.svg
        :target: https://travis-ci.org/AntoineCezar/flockcontext

.. image:: https://img.shields.io/badge/docs-latest-brightgreen.svg
        :target: http://flockcontext.readthedocs.org/

.. image:: https://img.shields.io/coveralls/AntoineCezar/flockcontext.svg
        :target: https://coveralls.io/github/AntoineCezar/flockcontext

.. image:: https://img.shields.io/pypi/v/flockcontext.svg
        :target: https://pypi.python.org/pypi/flockcontext


Improves `fcntl.flock <https://docs.python.org/library/fcntl.html#fcntl.flock>`_ usage.

``flock`` is a Unix command for `file locking <https://en.wikipedia.org/wiki/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
