SocketMutex

module for realize locking mechanism using sockets


Keywords
locking, lock, mutex, socket, run-once
License
MIT
Install
pip install SocketMutex==0.1

Documentation

SocketMutex

SocketMutex python3 module for realize locking mechanism using sockets

Requirements

  • socket (build-in)
  • sys (build-in)

Details

Preparations

Install Mutex using pip:

$ pip install [--user] socketmutex

Usage

Import module:

from SocketMutex import Mutex

Example 1

# import module
import os  # just to determine script name
from SocketMutex import Mutex

# create killer
mutex = Mutex(f"{os.path.basename(__file__)}")  # using script name as market for mutex for python 3.6 or above
mutex = Mutex('{}'.format(os.path.basename(__file__)))  # using script name as market for mutex for python 3.5 and older

# try to set lock and continue, otherwise do something else
if mutex.set_lock():
    # do stuff
else:
    # do another stuff

# try to release lock and continue, otherwise do something else
if mutex.release_lock():
    # do stuff
else:
    # do another stuff