@@ -10,7 +10,8 @@ simple way of inter-process communication:
10
10
11
11
lock = FileLock(" high_ground.txt.lock" )
12
12
with lock:
13
- open (" high_ground.txt" , " a" ).write(" You were the chosen one." )
13
+ with open (" high_ground.txt" , " a" ) as f:
14
+ f.write(" You were the chosen one." )
14
15
15
16
**Don't use ** a :class: `FileLock <filelock.FileLock> ` to lock the file you want to write to, instead create a separate
16
17
``.lock `` file as shown above.
@@ -59,11 +60,13 @@ locks:
59
60
.. code-block :: python
60
61
61
62
with lock:
62
- open (file_path, " a" ).write(" Hello there!" )
63
+ with open (file_path, " a" ) as f:
64
+ f.write(" Hello there!" )
63
65
64
66
lock.acquire()
65
67
try :
66
- open (file_path, " a" ).write(" General Kenobi!" )
68
+ with open (file_path, " a" ) as f:
69
+ f.write(" General Kenobi!" )
67
70
finally :
68
71
lock.release()
69
72
@@ -82,7 +85,8 @@ acquired within ``timeout`` seconds, a :class:`Timeout <filelock.Timeout>` excep
82
85
83
86
try :
84
87
with lock.acquire(timeout = 10 ):
85
- open (file_path, " a" ).write(" I have a bad feeling about this." )
88
+ with open (file_path, " a" ) as f:
89
+ f.write(" I have a bad feeling about this." )
86
90
except Timeout:
87
91
print (" Another instance of this application currently holds the lock." )
88
92
@@ -92,12 +96,14 @@ The lock objects are recursive locks, which means that once acquired, they will
92
96
93
97
def cite1 ():
94
98
with lock:
95
- open (file_path, " a" ).write(" I hate it when he does that." )
99
+ with open (file_path, " a" ) as f:
100
+ f.write(" I hate it when he does that." )
96
101
97
102
98
103
def cite2 ():
99
104
with lock:
100
- open (file_path, " a" ).write(" You don't want to sell me death sticks." )
105
+ with open (file_path, " a" ) as f:
106
+ f.write(" You don't want to sell me death sticks." )
101
107
102
108
103
109
# The lock is acquired here.
0 commit comments