wolph / portalocker

An easy library for Python file locking. It works on Windows, Linux, BSD and Unix systems and can even perform distributed locking. Naturally it also supports the with statement.
http://portalocker.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
265 stars 51 forks source link

How to remove the .lock file after the unlock? #28

Closed joanlopez closed 7 years ago

joanlopez commented 7 years ago

Hi,

I'm currently using this library in a Python application.

It is working quite good, but although it works, it does not remove the .lock file after the unlock.

There's any way/option to do it? The existence of this file while there are no locking processes is quite confusing.

Thanks.

wolph commented 7 years ago

Can you show the code you are using? Generally you would lock the actual file instead of create a lockfile using this application.

If you require this sort of operation I could easily create a utility function to handle this of course :)

joanlopez commented 7 years ago

Our project is available here: https://earth.bsc.es/gitlab/es/autosubmit

I think this incoherence is happening because we are not doing the proper usage of the library.

Our tool is intended to run different experiments in different supercomputers (workflow manager). In this case, our experiments are composed by some configuration files, some temporal files, etc. This is why we are using a fictional .lock file inside the experiments's folder. For example, to prevent running the same experiment twice at the same time (what could produce internal incoherences).

What could be a proper way of usage for this purpose?

wolph commented 7 years ago

There isn't one I suppose :)

It's a use-case I haven't considered before but I'm implementing a new utility function as we speak which will do the trick for you

wolph commented 7 years ago

I've added a utility class to easily create a temporary lockfile which will always* disappear after unlocking.

You can use it like this:

import portalocker

with portalocker.TemporaryFileLock(optional_filename_here):
    # do something here
    pass

Or:

import portalocker

lock = portalocker.TemporaryFileLock()
lock.acquire()

# do something here
# the lock automatically gets cleaned when the program exits but manually unlocking is possible:
lock.release()

Can you give it a try?

*when the process is killed using "kill -9" it won't have the chance to cleanup.