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

Donn Cave donn at u.washington.edu
Wed Oct 25 19:00:26 EDT 2000


Quoth "Joshua Muskovitz" <josh at open.com>:
| "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.
|
| This would be great, but on my Win2k box, I can open up the same file for
| write in two separate Python instances at the same time.  :-(
|
| The second instance of open("foo","w") does not fail.

What happens here?

    fd = os.open('foo', os.O_CREAT|os.O_EXCL|os.O_WRONLY)
    ... repeat

That's a POSIXism, as you may have guessed.  The file object open()
isn't "exclusively" as suggested above, that's what the O_CREAT|O_EXCL
does.

I haven't been following, hope I'm not missing the point.  I don't
know how one would approach the subject question on Windows, probably
not this way but who knows.  On BeOS, if you go to the trouble to
link it as a separate executable (needs only main, total size 2715
bytes), you can set multiple-launch/single-launch flags for it and
the system handles this automatically.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list