From 420e3527455be97e9282fd6cfbb3e6b28f2da5f3 Mon Sep 17 00:00:00 2001 From: SantaSpeen Date: Wed, 2 Apr 2025 13:53:27 +0300 Subject: [PATCH] [+] examples --- examples/acquire.py | 16 ++++++++++++++++ examples/find_index.py | 32 ++++++++++++++++++++++++++++++++ examples/simple.py | 10 ++++++++++ main.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 examples/acquire.py create mode 100644 examples/find_index.py create mode 100644 examples/simple.py create mode 100644 main.py diff --git a/examples/acquire.py b/examples/acquire.py new file mode 100644 index 0000000..3c06f38 --- /dev/null +++ b/examples/acquire.py @@ -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...") diff --git a/examples/find_index.py b/examples/find_index.py new file mode 100644 index 0000000..48ef8ad --- /dev/null +++ b/examples/find_index.py @@ -0,0 +1,32 @@ +from pywinmutex import WindowsMutex + +mutex_name = "anidev.myapp.mutex.{}" # Name constructor +mutex = WindowsMutex(mutex_name.format(0), True) + +# [W] Mutex() already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.1' +# [W] Mutex() already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.2' +# [I] Mutex() 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() + diff --git a/examples/simple.py b/examples/simple.py new file mode 100644 index 0000000..41138e1 --- /dev/null +++ b/examples/simple.py @@ -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...") diff --git a/main.py b/main.py new file mode 100644 index 0000000..48ef8ad --- /dev/null +++ b/main.py @@ -0,0 +1,32 @@ +from pywinmutex import WindowsMutex + +mutex_name = "anidev.myapp.mutex.{}" # Name constructor +mutex = WindowsMutex(mutex_name.format(0), True) + +# [W] Mutex() already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.1' +# [W] Mutex() already exists. Reinitializing mutex with a new name: 'anidev.myapp.mutex.2' +# [I] Mutex() 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() +