-1

I have multiple python scripts which have to write text on same file. Obviously I want to guarantee that data will be consistent and non-overlapped. Which is the simplest (and most efficient) way to do that? I don't need cross-platform solution.

Thank you

Akinn
  • 1,896
  • 4
  • 23
  • 36
  • How are they writing to the file? Are they each going to be appending to the end, rewriting from the beginning, or randomly somewhere in between? If only appending I don't think there would be an issue – MrJLP May 22 '17 at 18:43
  • They have to delete last char, append new text and close the file. If I use f.open(path,"a+") and f.write("text"). My doubt are about what happens if while script A is writing on file, script B do same? If there isn't a "locker" text could be overlap right? – Akinn May 22 '17 at 18:51
  • You could use a file lock. – MrJLP May 22 '17 at 18:53
  • I hoped in a simpler solution, because I haven't platform independent requirements... but it seems to be the only option available. – Akinn May 22 '17 at 19:01
  • 1
    Possible duplicate of [Python Multiple users append to the same file at the same time](https://stackoverflow.com/questions/11853551/python-multiple-users-append-to-the-same-file-at-the-same-time) –  May 22 '17 at 19:46

3 Answers3

3

Assuming you're on a *nix platform you can create a file lock using the fcntl. There is also the platform independent filelock, but I've never used that myself.

Using fcntl you would lock the file as soon as it was opened, then write, then release the lock.

MrJLP
  • 978
  • 6
  • 14
  • It seems what I need. Now I'm trying with f.open(path,'a') fcntl.flock(f,LOCK.EX) f.write("") fcntl.flock(f,LOCK.UN) f.close() But now I have another question... If a script has exclusive access and another one try to obtain the same what happens? I would like it retry continuously in a kind of priority queue... but I think in this way raise just an exception. – Akinn May 22 '17 at 19:38
  • 1
    Look at the docs for the underlying flock() call. If using LOCK_NB the call will be non-blocking and will raise an exception in python, otherwise it will block. – MrJLP May 22 '17 at 19:48
0

Use filelock mudule.

from filelock import FileLock

with FileLock("mySharedFile.txt"):
    # your code
    print("Locked.")
Vaibhav Rai
  • 183
  • 1
  • 8
-1

In the open(*filename,mode*) line, just use a as mode. This will make it add the lines you write instead of deleting the old ones

nighmared
  • 88
  • 8