The Mutex class

class Mutex(name=None)

Bases: winsys.core._WinSysObject

A Mutex is a kernel object which can only be held by one thread or process at a time. Its usual application is to protect shared data structures or to prevent more than one instance of an application from running simultaneously. Mutexes can be named or anonymous. Anonymous mutexes can be used between processes by passing their handle from one process to the other on the command line.

This is very similar to a Python threading.Lock object. (In fact the Python objects are implemented as Semaphores on Windows, presumably for re-entrancy). For this reason, the acquire() and release() names have been used for methods.

The mutex is its own context manager, so a typical usage would be:

from winsys import ipc

with ipc.mutex ("ONLYONCE"):
  # do stuff
acquire(timeout_ms=-1)

Acquire the mutex waiting for timeout_ms milliseconds before failing

Parameters:timeout_ms – how many milliseconds to wait before giving up
Raises :x_ipc_timeout if timeout expires
as_string()
pyobject()
release()

Release the mutex. Consider using the object as a context manager instead.

Previous topic

The Event class

Next topic

The Pipe classes

This Page