Skip to main content
=======

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org>

Download-URL: https://github.com/benediktschmitt/py-filelock/archive/master.zip
Description: py-filelock
===========

.. image:: https://travis-ci.org/benediktschmitt/py-filelock.svg?branch=master
:target: https://travis-ci.org/benediktschmitt/py-filelock

`Similar Libraries <#similar-libraries>`_
~ `Installation <#installation>`_
~ `Documentation <#documentation>`_
~ `Contributions <#contributions>`_
~ `License <#license>`_

This package contains a single module, which implements a platform independent
file lock in Python, which provides a simple way of inter-process communication:

.. code-block:: python

from filelock import Timeout, FileLock

lock = FileLock("high_ground.txt.lock")
with lock:
open("high_ground.txt", "a").write("You were the chosen one.")

**Don't use** a *FileLock* to lock the file you want to write to, instead create a separate
*.lock* file as shown above.

.. image:: https://raw.githubusercontent.com/benediktschmitt/py-filelock/master/example/example.gif

`Animated example <https://github.com/benediktschmitt/py-filelock/blob/master/example/example.gif>`_

Similar libraries
-----------------

Perhaps you are looking for something like

* https://pypi.python.org/pypi/pid/2.1.1
* https://docs.python.org/3.6/library/msvcrt.html#msvcrt.locking
* or https://docs.python.org/3/library/fcntl.html#fcntl.flock

Installation
------------

*py-filelock* is available via PyPi:

.. code-block:: bash

$ pip3 install filelock

Documentation
-------------

The documentation for the API is available on
`readthedocs.org <https://filelock.readthedocs.io/>`_.

Examples
^^^^^^^^

A *FileLock* is used to indicate another process of your application that a resource or working
directory is currently used. To do so, create a *FileLock* first:

.. code-block:: python

from filelock import Timeout, FileLock

file_path = "high_ground.txt"
lock_path = "high_ground.txt.lock"

lock = FileLock(lock_path, timeout=1)

The lock object supports multiple ways for acquiring the lock, including the ones used to acquire
standard Python thread locks:

.. code-block:: python

with lock:
open(file_path, "a").write("Hello there!")

lock.acquire()
try:
open(file_path, "a").write("General Kenobi!")
finally:
lock.release()

The *acquire()* method accepts also a *timeout* parameter. If the lock cannot be acquired
within *timeout* seconds, a *Timeout* exception is raised.:

.. code-block:: python

try:
with lock.acquire(timeout=10):
open(file_path, "a").write("I have a bad feeling about this.")
except Timeout:
print("Another instance of this application currently holds the lock.")

The lock objects are recursive locks, which means that once acquired, they will not block on
successive lock requests:

.. code-block:: python

def cite1():
with lock:
open(file_path, "a").write("I hate it when he does that.")

def cite2():
with lock:
open(file_path, "a").write("You don't want to sell me death sticks.")

# The lock is acquired here.
with lock:
cite1()
cite2()

# And released here.

FileLock vs SoftFileLock
^^^^^^^^^^^^^^^^^^^^^^^^

The *FileLock* is platform dependent while the *SoftFileLock* is not. Use the *FileLock* if all
instances of your application are running on the same host and a *SoftFileLock* otherwise.

The *SoftFileLock* only watches the existence of the lock file. This makes it ultra portable, but
also more prone to dead locks if the application crashes. You can simply delete the lock file in
such cases.

Contributions
-------------

Contributions are always welcome. Never hesitate to open a new issue.

License
-------

This package is `public domain <LICENSE.rst>`_.

Platform: UNKNOWN
Classifier: License :: Public Domain
Classifier: Development Status :: 5 - Production/Stable
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Intended Audience :: Developers
Classifier: Topic :: System
Classifier: Topic :: Internet
Classifier: Topic :: Software Development :: Libraries

Metadata

Release files for filelock 3.0.8

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

Source distribution (sdist)

Source distribution for filelock 3.0.8
File Size Uploaded
filelock-3.0.8.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for filelock 3.0.8
File Interpreter ABI Platform
filelock-3.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 14.1 kB

Release files / filelock-3.0.8.tar.gz

Download URL filelock-3.0.8.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
43531c580b8ad7bc830b5ea921b6aca8197d2d982b67b940359d9b4d546e5a9a
BLAKE2b-256 checksum
How to use checksums
ea656b62e9fa0cf2a1d564bab314e89f26f04162e3c6adb08f2b7584805c5335
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.9.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.2

Release files / filelock-3.0.8-py3-none-any.whl

Download URL filelock-3.0.8-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2631e0beb9b3095510c2c25b10a5f3011f65e8d6ff9a145799a27d03dfa70ee
BLAKE2b-256 checksum
How to use checksums
851c389ca4da8b631a06dec64c94c9c6f22bbd9be236f0030ee4863e7d6e42a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.9.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.5.2

Release history Release notifications | RSS feed

4.1.1

2 release files

4.1.0

2 release files

4.0.9

2 release files

4.0.8

2 release files

4.0.7

2 release files

4.0.6

2 release files

4.0.5

2 release files

4.0.4

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.32.7

2 release files

3.32.5

2 release files

3.32.4

2 release files

3.32.3

2 release files

3.32.2

2 release files

3.32.0

2 release files

3.31.2

2 release files

3.31.1

2 release files

3.31.0

2 release files

3.30.3

2 release files

3.30.2

2 release files

3.30.1

2 release files

3.30.0

2 release files

3.29.4

2 release files

3.29.3

2 release files

3.29.2

2 release files

3.29.0

2 release files

3.28.0

2 release files

3.25.2

2 release files

3.24.3

2 release files

3.24.2

2 release files

3.24.1

2 release files

3.24.0

2 release files

3.23.0

2 release files

3.22.0

2 release files

3.21.2

2 release files

3.21.1

2 release files

3.21.0

2 release files

3.20.4

2 release files

3.20.1

2 release files

3.19.1

2 release files

3.18.0

2 release files

3.17.0

2 release files

3.16.1

2 release files

3.15.4

2 release files

3.15.3

2 release files

3.15.2

2 release files

3.15.1

2 release files

3.15.0

2 release files

3.14.0

2 release files

3.13.3

2 release files

3.13.2

2 release files

3.13.1

2 release files

3.13.0

2 release files

3.12.4

2 release files

3.12.3

2 release files

3.12.2

2 release files

3.12.1

2 release files

3.12.0

2 release files

3.10.7

2 release files

3.10.6

2 release files

3.10.5

2 release files

3.10.4

2 release files

3.10.3

2 release files

3.10.2

2 release files

3.10.1

2 release files

3.10.0

2 release files

3.9.1

2 release files

3.9.0

2 release files

3.8.2

2 release files

3.8.1

2 release files

3.8.0

2 release files

3.7.1

2 release files

3.7.0

2 release files

3.6.0

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.4.2

2 release files

3.4.1

2 release files

3.4.0

2 release files

3.3.2

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.0

2 release files

3.0.12

2 release files

3.0.9

2 release files

This release

3.0.8 This release

2 release files

3.0.6

2 release files

3.0.4

1 release file

3.0.3

1 release file

3.0.2

1 release file

3.0.0

1 release file

2.0.13

1 release file

2.0.12

1 release file

2.0.11

1 release file

2.0.10

1 release file

2.0.9

1 release file

2.0.8

1 release file

2.0.7

1 release file

2.0.6

1 release file

2.0.5

1 release file

2.0.4

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

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