Consider those two python programs:
script_a.py
:
from datetime import datetime
from time import sleep
while True:
sleep(1)
with
You are allowed to open a file as many times as you want, so long as the operating system doesn't stop you. This is occasionally useful to get multiple cursors into a file for complex operations.
The reason that script_b.py
thinks that the file is empty is that the file is empty:
with open('foo.txt', 'w') as f:
opening a file in w
mode immediately erases (i.e. truncates) the file. There's an initial three second gap in script_a
where the file is completely 100% empty, and that's what script_b
sees.
In the next three second gap after you call f.write
, the file is still... probably empty. This is due to buffering - the file on disk is not guaranteed to contain everything that you have written to it with write
until you either close
(i.e. exit the context manager block) or manually invoke flush
on the file handle.
Alternatively, you can open in unbuffered mode, so that writes are always immediately written to disk.
with open('foo.txt','w',0) as f:
#no buffering, f.write() writes immediately to disk
See the other answer and comments regarding how multiple file opens work in Python. If you've read all that, and still want to lock access to the file on a POSIX platform, then you can use the fcntl library.
Keep in mind that: A) other programs may ignore your lock on the file, B) some networked file systems don't implement locking very well, or at all C) be sure to be very careful to release locks and avoid deadlock as flock won't detect it [1][2].
Example.... script_a.py
from datetime import datetime
from time import sleep
import fcntl
while True:
sleep(1)
with open('foo.txt', 'w') as f:
s = str(datetime.now())
print datetime.now(), "Waiting for lock"
fcntl.flock(f, fcntl.LOCK_EX)
print datetime.now(), "Lock clear, writing"
sleep(3)
f.write(s)
print datetime.now(), "releasing lock"
fcntl.flock(f, fcntl.LOCK_UN)
script_b.py
import fcntl
from datetime import datetime
while True:
with open('foo.txt') as f:
print datetime.now(), "Getting lock"
fcntl.flock(f, fcntl.LOCK_EX)
print datetime.now(), "Got lock, reading file"
s = f.read()
print datetime.now(), "Read file, releasing lock"
fcntl.flock(f, fcntl.LOCK_UN)
print s
Hope this helps!