Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file object treatment in docs #130

Merged
merged 1 commit into from Feb 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -74,7 +77,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 @@ -84,12 +88,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