|
4 | 4 | [](https://pypi.org/project/filelock/) |
6 | 6 | [](https://filelock.readthedocs.io/en/latest/?badge=latest) |
| 7 | +status](https://readthedocs.org/projects/py-filelock/badge/?version=latest&style=flat-square)](https://py-filelock.readthedocs.io/en/latest/?badge=latest) |
8 | 8 | [](https://github.com/psf/black) |
10 | 10 | [](https://pepy.tech/project/filelock/month) |
11 | 11 | [](https://github.com/tox-dev/py-filelock/actions/workflows/check.yml) |
12 | 12 |
|
13 | | -This package contains a single module, which implements a platform independent file lock in Python, which provides a |
14 | | -simple way of inter-process communication: |
15 | | - |
16 | | -```Python |
17 | | -from filelock import Timeout, FileLock |
18 | | - |
19 | | -lock = FileLock("high_ground.txt.lock") |
20 | | -with lock: |
21 | | - open("high_ground.txt", "a").write("You were the chosen one.") |
22 | | -``` |
23 | | - |
24 | | -**Don't use** a _FileLock_ to lock the file you want to write to, instead create a separate _.lock_ file as shown above. |
25 | | - |
26 | | - |
27 | | - |
28 | | -## Similar libraries |
29 | | - |
30 | | -Perhaps you are looking for something like: |
31 | | - |
32 | | -- the [pid](https://pypi.python.org/pypi/pid) 3rd party library, |
33 | | -- for Windows the [msvcrt](https://docs.python.org/3/library/msvcrt.html#msvcrt.locking) module in the standar library, |
34 | | -- for UNIX the [fcntl](https://docs.python.org/3/library/fcntl.html#fcntl.flock) module in the standard library. |
35 | | - |
36 | | -## Installation |
37 | | - |
38 | | -_py-filelock_ is available via PyPi: |
39 | | - |
40 | | -```bash |
41 | | -python -m pip install filelock |
42 | | -``` |
43 | | - |
44 | | -## Documentation |
45 | | - |
46 | | -The documentation for the API is available on [readthedocs.org](https://filelock.readthedocs.io/). |
47 | | - |
48 | | -### Examples |
49 | | - |
50 | | -A _FileLock_ is used to indicate another process of your application that a resource or working directory is currently |
51 | | -used. To do so, create a _FileLock_ first: |
52 | | - |
53 | | -```python |
54 | | -from filelock import Timeout, FileLock |
55 | | - |
56 | | -file_path = "high_ground.txt" |
57 | | -lock_path = "high_ground.txt.lock" |
58 | | - |
59 | | -lock = FileLock(lock_path, timeout=1) |
60 | | -``` |
61 | | - |
62 | | -The lock object supports multiple ways for acquiring the lock, including the ones used to acquire standard Python thread |
63 | | -locks: |
64 | | - |
65 | | -```python |
66 | | -with lock: |
67 | | - open(file_path, "a").write("Hello there!") |
68 | | - |
69 | | -lock.acquire() |
70 | | -try: |
71 | | - open(file_path, "a").write("General Kenobi!") |
72 | | -finally: |
73 | | - lock.release() |
74 | | -``` |
75 | | - |
76 | | -The _acquire()_ method accepts also a _timeout_ parameter. If the lock cannot be acquired within _timeout_ seconds, a |
77 | | -_Timeout_ exception is raised: |
78 | | - |
79 | | -```python |
80 | | -try: |
81 | | - with lock.acquire(timeout=10): |
82 | | - open(file_path, "a").write("I have a bad feeling about this.") |
83 | | -except Timeout: |
84 | | - print("Another instance of this application currently holds the lock.") |
85 | | -``` |
86 | | - |
87 | | -The lock objects are recursive locks, which means that once acquired, they will 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 | | -All log messages by this library are made using the _DEBUG_ level, under the `filelock` name. On how to control |
107 | | -displaying/hiding that please consult the |
108 | | -[logging documentation of the standard library](https://docs.python.org/3/howto/logging.html). |
109 | | - |
110 | | -E.g. to hide these messages you can use `logging.getLogger("filelock").setLevel(logging.INFO)`. |
111 | | - |
112 | | -## FileLock vs SoftFileLock |
113 | | - |
114 | | -The _FileLock_ is platform dependent while the _SoftFileLock_ is not. Use the _FileLock_ if all instances of your |
115 | | -application are running on the same host and a _SoftFileLock_ otherwise. |
116 | | - |
117 | | -The _SoftFileLock_ only watches the existence of the lock file. This makes it ultra portable, but also more prone to |
118 | | -dead locks if the application crashes. You can simply delete the lock file in such cases. |
119 | | - |
120 | | -## Contributions |
121 | | - |
122 | | -Contributions are always welcome, please make sure they pass all tests before creating a pull request. Never hesitate to |
123 | | -open a new issue, although it may take some time for me to respond. |
124 | | - |
125 | | -## License |
126 | | - |
127 | | -This package is [public domain](./LICENSE). |
| 13 | +For more information checkout the [official documentation](https://py-filelock.readthedocs.io/en/latest/api.html). |
0 commit comments