如何为单实例 Python 应用程序创建互斥体
我一直在使用以下配方来仅允许我的应用程序的单个实例: http://code.activestate.com/recipes/474070- create-a-single-instance-application/
该配方使用一个“mutexname”变量,该变量填充了一些互斥值。如果我想创建自己独特的“互斥体”,我该怎么做?另外,这个食谱如何发挥作用?谁能解释一下吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该配方使用 win32 api 函数 CreateMutex 创建一个命名互斥体。互斥锁是一个系统对象,存在于应用程序进程之外。当使用已存在的名称调用 CreateMutex 时,GetLastError 返回 ERROR_ALREADY_EXISTS。该配方检查此返回值,如果是则退出。您应该更改配方以使用不同的字符串作为互斥体名称。选择其他人不太可能锁定的东西,因为如果他们这样做,您的应用程序将无法启动。
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.