4

I have downloaded the filelock module in order to lock files using my python program. this is my code:

import filelock
lock = filelock.FileLock(path)
lock.acquire()
#do something...
lock.release()

for some reason I don't understand when I release the lock the file is deleted. Does anyone know how to deal with this? How can I keep the file available also after the lock is released? if it is relevant, my file is kept on a separate hard drive. thank you.

I am using windows 10 pro

Isdj
  • 1,835
  • 1
  • 18
  • 36
  • Which operating system are you doing this on? – siphr Feb 29 '16 at 12:27
  • @PawełKordowski context manager? – Isdj Feb 29 '16 at 12:28
  • see http://stackoverflow.com/a/498505/2999347 – Paweł Kordowski Feb 29 '16 at 12:32
  • Note: The "path" passed to file lock is not the actual file you want to control access-to. It's an arbitrary path that symbolically represents some resource being locked. In many cases, if the resource is a file or directory, the lock path has the same name, but a `.lock` extension, but this is just convention. In other cases the lock goes under /var/lock/SOMEWHERE. Remember that this is "advisory locking": all code that accesses the file you want to protect must use the same lockfile path. The lockfile path can even represent arbitrary things that you do not want to occur in parallel. – Scott Smith Feb 25 '21 at 21:40

2 Answers2

3

I am not sure why but the documentation suggests that it should not do what you are experiencing. However if you are using this on windows, you can look at the implementation of release lock and you will realise that it will indeed delete the file if it is the final lock or if the lock is forcefully released. Please have a look at the section for the "Windows locking mechanism".

The underlying implementation for windows locking uses the following release code:

def _release(self):
    msvcrt.locking(self._lock_file_fd, msvcrt.LK_UNLCK, 1)
    os.close(self._lock_file_fd)
    self._lock_file_fd = None

    try:
        os.remove(self._lock_file)
    # Probably another instance of the application
    # that acquired the file lock.
    except OSError:
        pass
    return None

As you can see the os.remove will delete the file. Even though this does not help but hopefully explains why this is happening. Could be a bug or stale code somebody forgot to remove.

siphr
  • 421
  • 4
  • 12
  • Do you know of a way I can override this? I cant keep the files in temp because they are big.. – Isdj Feb 29 '16 at 12:41
  • Now, why would that happen? That seems so odd...I tested on my Mac and everything works fine. File is still there. – idjaw Feb 29 '16 at 12:41
  • 2
    @IsaacDj I suggest the best way is to discuss this further with the module developers. It may turn out that it is a defect that might have overlooked or atleast you might get a reasonable explanation as to why it is the way it is and how you could work around it. – siphr Feb 29 '16 at 13:28
  • @siphr what is the reason that the lock also erases the file contents? – Isdj Feb 29 '16 at 14:54
  • If you mean that when the lock is created or acquired that it empties the contents of the file well that is because the module creates the lock file with the truncate flag. Which means that a file if already present will always be truncated (emptied). – siphr Feb 29 '16 at 15:10
1

This worked for me in Windows and should also work for you benediktschmitt

Hi,

the use case for this library is signalizing different instances of an application, that shared resources are currently accessed. For example: Some synchronization programs create a lock file during synchronization in the root folder to prevent other instances from doing it to the same time. As soon as the lock file has been removed, the other instance starts the synchronization progress.

If you want to avoid race conditions, you can use either this library like this:

lock = FileLock(flnm + ".lock") with lock.acquire(timeout=5):
with open(flnm, "a") as file: file.write("some text")

or you take a look at the underlying os functions: https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch04s25.html

EDIT: Removing the file is done intentionally as part of the clean up.

Prophet Daniel
  • 327
  • 2
  • 15