|
| 1 | +# py-filelock |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This package contains a single module, which implements a platform independent |
| 6 | +file lock in Python, which provides a simple way of inter-process communication: |
| 7 | + |
| 8 | +```Python |
| 9 | +from filelock import Timeout, FileLock |
| 10 | + |
| 11 | +lock = FileLock("high_ground.txt.lock") |
| 12 | +with lock: |
| 13 | + open("high_ground.txt", "a").write("You were the chosen one.") |
| 14 | +``` |
| 15 | + |
| 16 | +**Don't use** a *FileLock* to lock the file you want to write to, instead create |
| 17 | +a separate *.lock* file as shown above. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Similar libraries |
| 23 | + |
| 24 | +Perhaps you are looking for something like |
| 25 | + |
| 26 | +* https://pypi.python.org/pypi/pid/2.1.1 |
| 27 | +* https://docs.python.org/3.6/library/msvcrt.html#msvcrt.locking |
| 28 | +* or https://docs.python.org/3/library/fcntl.html#fcntl.flock |
| 29 | + |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +*py-filelock* is available via PyPi: |
| 34 | + |
| 35 | +``` |
| 36 | +$ pip3 install filelock |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +## Documentation |
| 41 | + |
| 42 | +The documentation for the API is available on |
| 43 | +[readthedocs.org](https://filelock.readthedocs.io/). |
| 44 | + |
| 45 | + |
| 46 | +### Examples |
| 47 | + |
| 48 | +A *FileLock* is used to indicate another process of your application that a |
| 49 | +resource or working |
| 50 | +directory is currently used. To do so, create a *FileLock* first: |
| 51 | + |
| 52 | +```Python |
| 53 | +from filelock import Timeout, FileLock |
| 54 | + |
| 55 | +file_path = "high_ground.txt" |
| 56 | +lock_path = "high_ground.txt.lock" |
| 57 | + |
| 58 | +lock = FileLock(lock_path, timeout=1) |
| 59 | +``` |
| 60 | + |
| 61 | +The lock object supports multiple ways for acquiring the lock, including the |
| 62 | +ones used to acquire standard Python thread locks: |
| 63 | + |
| 64 | +```Python |
| 65 | +with lock: |
| 66 | + open(file_path, "a").write("Hello there!") |
| 67 | + |
| 68 | +lock.acquire() |
| 69 | +try: |
| 70 | + open(file_path, "a").write("General Kenobi!") |
| 71 | +finally: |
| 72 | + lock.release() |
| 73 | +``` |
| 74 | + |
| 75 | +The *acquire()* method accepts also a *timeout* parameter. If the lock cannot be |
| 76 | +acquired within *timeout* seconds, a *Timeout* exception is raised: |
| 77 | + |
| 78 | +```Python |
| 79 | +try: |
| 80 | + with lock.acquire(timeout=10): |
| 81 | + open(file_path, "a").write("I have a bad feeling about this.") |
| 82 | +except Timeout: |
| 83 | + print("Another instance of this application currently holds the lock.") |
| 84 | +``` |
| 85 | + |
| 86 | +The lock objects are recursive locks, which means that once acquired, they will |
| 87 | +not block on successive lock requests: |
| 88 | + |
| 89 | +```Python |
| 90 | +def cite1(): |
| 91 | + with lock: |
| 92 | + open(file_path, "a").write("I hate it when he does that.") |
| 93 | + |
| 94 | +def cite2(): |
| 95 | + with lock: |
| 96 | + open(file_path, "a").write("You don't want to sell me death sticks.") |
| 97 | + |
| 98 | +# The lock is acquired here. |
| 99 | +with lock: |
| 100 | + cite1() |
| 101 | + cite2() |
| 102 | + |
| 103 | +# And released here. |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | +## FileLock vs SoftFileLock |
| 108 | + |
| 109 | +The *FileLock* is platform dependent while the *SoftFileLock* is not. Use the |
| 110 | +*FileLock* if all instances of your application are running on the same host and |
| 111 | +a *SoftFileLock* otherwise. |
| 112 | + |
| 113 | +The *SoftFileLock* only watches the existence of the lock file. This makes it |
| 114 | +ultra portable, but also more prone to dead locks if the application crashes. |
| 115 | +You can simply delete the lock file in such cases. |
| 116 | + |
| 117 | + |
| 118 | +## Contributions |
| 119 | + |
| 120 | +Contributions are always welcome, please make sure they pass all tests before |
| 121 | +creating a pull request. Never hesitate to open a new issue, although it may |
| 122 | +take some time for me to respond. |
| 123 | + |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +This package is [public domain](./LICENSE.rst). |
0 commit comments