I have a file xxx.txt that is accessed by other Python files. When script_1 is about to write to xxx.txt it should lock the file and script_2 should wait until the file is released by script_1.
I tried using this code but it does not lock the file. script_2 was able to write the file.
import fcntl, os, signal, time
os.fork()
class TimeoutException(Exception): pass
def signal_handler(signum, frame):
    raise TimeoutException()
f = os.open("xxx.txt", os.O_RDWR|os.O_CREAT)
fcntl.flock(f, fcntl.LOCK_EX)
time.sleep(20)
os.write(f, bytes("oook_0", "utf-8"))
fcntl.flock(f, fcntl.LOCK_UN)
os.close(f)
 
     
    