Skip to content

Commit b99d5e0

Browse files
author
Benedikt Schmitt
committed
*added* test.py module to MANIFEST and made tests available in the setup commands (see issue #48)
*changed* the README from reStructuredText to Markdown in the hope the PyPi parser would not complain anymore (no luck)
1 parent 71a5e02 commit b99d5e0

File tree

6 files changed

+134
-134
lines changed

6 files changed

+134
-134
lines changed

LICENSE.rst renamed to LICENSE

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
License
2-
=======
3-
41
This is free and unencumbered software released into the public domain.
52

63
Anyone is free to copy, modify, publish, use, compile, sell, or

MANIFEST.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
include LICENSE.rst
2-
include README.rst
1+
include LICENSE
2+
include README.md
3+
include test.py

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# py-filelock
2+
3+
![travis-ci](https://travis-ci.org/benediktschmitt/py-filelock.svg?branch=master)
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+
![animated example](https://raw.githubusercontent.com/benediktschmitt/py-filelock/master/example/example.gif)
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).

README.rst

Lines changed: 0 additions & 127 deletions
This file was deleted.

filelock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"FileLock"
7070
]
7171

72-
__version__ = "3.0.10"
72+
__version__ = "3.0.11"
7373

7474

7575
_logger = None

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# Main
3535
# ------------------------------------------------
3636
try:
37-
long_description = open("README.rst").read()
37+
long_description = open("README.md").read()
3838
except OSError:
3939
long_description = "not available"
4040

@@ -48,12 +48,14 @@
4848
version = __version__,
4949
description = "A platform independent file lock.",
5050
long_description = long_description,
51+
long_description_content_type = "text/markdown",
5152
author = "Benedikt Schmitt",
5253
author_email = "[email protected]",
5354
url = "https://github.com/benediktschmitt/py-filelock",
5455
download_url = "https://github.com/benediktschmitt/py-filelock/archive/master.zip",
5556
py_modules = ["filelock"],
5657
license = license_,
58+
test_suite="test",
5759
classifiers = [
5860
"License :: Public Domain",
5961
"Development Status :: 5 - Production/Stable",

0 commit comments

Comments
 (0)