Skip to content

Commit

Permalink
Fix file object treatment in docs (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherArchivist committed Feb 15, 2022
1 parent 8947e72 commit 3db752f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions docs/index.rst
Expand Up @@ -10,7 +10,8 @@ simple way of inter-process communication:
lock = FileLock("high_ground.txt.lock")
with lock:
open("high_ground.txt", "a").write("You were the chosen one.")
with open("high_ground.txt", "a") as f:
f.write("You were the chosen one.")
**Don't use** a :class:`FileLock <filelock.FileLock>` to lock the file you want to write to, instead create a separate
``.lock`` file as shown above.
Expand Down Expand Up @@ -59,11 +60,13 @@ locks:
.. code-block:: python
with lock:
open(file_path, "a").write("Hello there!")
with open(file_path, "a") as f:
f.write("Hello there!")
lock.acquire()
try:
open(file_path, "a").write("General Kenobi!")
with open(file_path, "a") as f:
f.write("General Kenobi!")
finally:
lock.release()
Expand All @@ -82,7 +85,8 @@ acquired within ``timeout`` seconds, a :class:`Timeout <filelock.Timeout>` excep
try:
with lock.acquire(timeout=10):
open(file_path, "a").write("I have a bad feeling about this.")
with open(file_path, "a") as f:
f.write("I have a bad feeling about this.")
except Timeout:
print("Another instance of this application currently holds the lock.")
Expand All @@ -92,12 +96,14 @@ The lock objects are recursive locks, which means that once acquired, they will
def cite1():
with lock:
open(file_path, "a").write("I hate it when he does that.")
with open(file_path, "a") as f:
f.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.")
with open(file_path, "a") as f:
f.write("You don't want to sell me death sticks.")
# The lock is acquired here.
Expand Down

0 comments on commit 3db752f

Please sign in to comment.