mirror of
https://github.com/SantaSpeen/winmutex.git
synced 2026-10-26 13:49:58 +00:00
[+] examples
This commit is contained in:
1 parent
474bcdee5a
commit
420e352745
4 files changed
+90
No files matched your search
@@ -0,0 +1,16 @@
|
|||||||
|
from pywinmutex import WindowsMutex
|
||||||
|
|
||||||
|
mutex = WindowsMutex("anidev/pywinmutex/acquire", True) # Name may be any string
|
||||||
|
|
||||||
|
if not mutex.acquire(5000): # Acquire the mutex with a timeout of 5 seconds; None for no timeout
|
||||||
|
print(f"[W] Mutex({mutex}) already exists or acquire timeout exceeded.")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
# Do some work while holding the mutex
|
||||||
|
|
||||||
|
print(f"[I] Mutex({mutex}) acquired.")
|
||||||
|
input("Enter to release the mutex and exit> ")
|
||||||
|
|
||||||
|
# Release the mutex
|
||||||
|
mutex.release()
|
||||||
|
print(f"[I] Mutex({mutex}) released. Exiting...")
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
from pywinmutex import WindowsMutex
|
||||||
|
|
||||||
|
mutex_name = "anidev.myapp.mutex.{}" # Name constructor
|
||||||
|
mutex = WindowsMutex(mutex_name.format(0), True)
|
||||||
|
|
||||||
|
# [W] Mutex(<WindowsMutex(name='anidev.myapp.mutex.0', multiuser=True)>) already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.1'
|
||||||
|
# [W] Mutex(<WindowsMutex(name='anidev.myapp.mutex.1', multiuser=True)>) already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.2'
|
||||||
|
# [I] Mutex(<WindowsMutex(name='anidev.myapp.mutex.2', multiuser=True)>) acquired. Running the application...
|
||||||
|
# Enter to release the mutex and exit>
|
||||||
|
# Mutex released. Exiting...
|
||||||
|
|
||||||
|
def find_index():
|
||||||
|
index = 0
|
||||||
|
while True:
|
||||||
|
index += 1
|
||||||
|
if mutex.exist:
|
||||||
|
print(f"[W] Mutex({mutex}) already exists. Reinitializing mutex with a new name: {mutex_name.format(index)!r}")
|
||||||
|
mutex.reinint(mutex_name.format(index))
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with mutex:
|
||||||
|
print(f"[I] Mutex({mutex}) acquired. Running the application...")
|
||||||
|
input("Enter to release the mutex and exit> ")
|
||||||
|
|
||||||
|
print("Mutex released. Exiting...")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
find_index()
|
||||||
|
main()
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
from pywinmutex import WindowsMutex
|
||||||
|
|
||||||
|
mutex = WindowsMutex("anidev/pywinmutex/simple", True) # Name may be any string
|
||||||
|
mutex.timeout = 2500 # Set a timeout of 2.5 seconds
|
||||||
|
|
||||||
|
with mutex:
|
||||||
|
print(f"[I] Mutex({mutex}) acquired.")
|
||||||
|
input("Enter to release the mutex and exit> ")
|
||||||
|
|
||||||
|
print(f"[I] Mutex({mutex}) released. Exiting...")
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
from pywinmutex import WindowsMutex
|
||||||
|
|
||||||
|
mutex_name = "anidev.myapp.mutex.{}" # Name constructor
|
||||||
|
mutex = WindowsMutex(mutex_name.format(0), True)
|
||||||
|
|
||||||
|
# [W] Mutex(<WindowsMutex(name='anidev.myapp.mutex.0', multiuser=True)>) already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.1'
|
||||||
|
# [W] Mutex(<WindowsMutex(name='anidev.myapp.mutex.1', multiuser=True)>) already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.2'
|
||||||
|
# [I] Mutex(<WindowsMutex(name='anidev.myapp.mutex.2', multiuser=True)>) acquired. Running the application...
|
||||||
|
# Enter to release the mutex and exit>
|
||||||
|
# Mutex released. Exiting...
|
||||||
|
|
||||||
|
def find_index():
|
||||||
|
index = 0
|
||||||
|
while True:
|
||||||
|
index += 1
|
||||||
|
if mutex.exist:
|
||||||
|
print(f"[W] Mutex({mutex}) already exists. Reinitializing mutex with a new name: {mutex_name.format(index)!r}")
|
||||||
|
mutex.reinint(mutex_name.format(index))
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with mutex:
|
||||||
|
print(f"[I] Mutex({mutex}) acquired. Running the application...")
|
||||||
|
input("Enter to release the mutex and exit> ")
|
||||||
|
|
||||||
|
print("Mutex released. Exiting...")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
find_index()
|
||||||
|
main()
|
||||||
|
|
||||||
Reference in new issue
Block a user