Sunjay Varma
Sunjay Varma

Reputation: 5115

How to Create a Mutex for a Single Instance Python Application

I have been using the following recipe to only allow a single instance of my application: http://code.activestate.com/recipes/474070-creating-a-single-instance-application/

The recipe uses a "mutexname" variable which is filled with some mutex value. If I want to create my own unique "mutex", how do I do it? Also, how does this recipe work? Can anyone explain?

Thanks

Upvotes: 0

Views: 4116

Answers (1)

Mike Zboray
Mike Zboray

Reputation: 40838

The recipe uses the win32 api function CreateMutex to create a named mutex. A mutex is a system object and exists outside your app's process. GetLastError returns ERROR_ALREADY_EXISTS when CreateMutex is called with a name that already exists. The recipe checks for this return value and exits if so. You should change the recipe to use a different string for mutexname. Pick something that is unlikely for some else to lock on because if they do your application won't be able to start.

Upvotes: 2

Related Questions