[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Cross platform mutex to prevent script running more than instance?

What about using flock()? I don't know if it works on Windows, but it works
really well for Unix/Linux systems.  I typically create a log file in a known
location using any atomic method that doesn't replace/overwrite a file, and
flock() it for the duration of the script.

Cem Karan

On Mon, Sep 3, 2018, 11:39 PM Cameron Simpson <cs at cskk.id.au> wrote:

> On 03Sep2018 07:45, Malcolm Greene <python at bdurham.com> wrote:
> >Use case: Want to prevent 2+ instances of a script from running ...
> >ideally in a cross platform manner. I've been researching this topic and
> >am surprised how complicated this capability appears to be and how the
> >diverse the solution set is. I've seen solutions ranging from using
> >directories, named temporary files,  named sockets/pipes, etc. Is there
> >any consensus on best practice here?
> I like os.mkdir of a known directory name. This tends to be atomic and
> forbidden when the name already exists, on all UNIX platforms, over remote
> filesystems. And, I expect, likewise on Windows.
> All the other modes like opening files O_EXCL etc tend to be platform
> specific
> and not reliable over network filesystems.
> And pid based approaches don't work cross machine, if that is an issue.
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
> --
> https://mail.python.org/mailman/listinfo/python-list