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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…