How do I force a single instance of a python app?

jay.krell at cornell.edu jay.krell at cornell.edu
Thu Oct 26 04:00:16 EDT 2000


>Or attempt to create a directory - that is an atomic operation that
>indicates what you need to know.  Obviously will need some sort of

> The second instance of open("foo","w") does not fail.

Creating a directory does not have automatic cleanup upon process death.

A real Win32 synchronization mechanism like a possibly named mutex is best,
but for the people that insist on files, use the Win32 api that lets you
specify no sharing.

The problem is "sharing". I don't know about Python, but in C++ you'd want:

CreateFile(
    foo, /* c:\foo, whatever */
    GENERIC_WRITE | GENERIC_READ
    0, /* no share */
    NULL, /* security */
    CREATE_NEW, /* fails if already existing */
    FILE_FLAG_DELETE_ON_CLOSE /* should be obvious */
    NULL /* "template file" */
    );

msvcrt also has _sopen for "share open" and maybe _sfopen or _fsopen that
also expose the extra parameter and look like Unix open or stdio fopen ...

 - Jay

-----Original Message-----
From: Mark Hammond <MarkH at ActiveState.com>
Newsgroups: comp.lang.python
To: python-list at python.org <python-list at python.org>
Date: Wednesday, October 25, 2000 5:24 PM
Subject: Re: How do I force a single instance of a python app?


>"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> wrote in message
>news:urodvssp7e7cmbv1ljlqnqgej9rjhfqbli at 4ax.com...
>> Open a flag file exclusively for writing. If successful you are
>alone,
>> if not, another instance already has the file.
>>
>> The OS should close the file and free the exclusive lock if the app
>> crashes.
>
>Or attempt to create a directory - that is an atomic operation that
>indicates what you need to know.  Obviously will need some sort of
>recovery options or process tho...
>
>Mark.
>
>
>--
>http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list