mutex — mutex module#

The mutex module provides a mutual-exclusion primitive for protecting critical sections of code or shared data from concurrent access by an interrupt service routine and the main thread.

Lock acquisition and release is performed via a context manager (with statement), which blocks until the mutex is available. A non-blocking Mutex.test() method is also provided.

Example:

from mutex import Mutex

mtx = Mutex()

# Acquire for a critical section (blocks if already held).
with mtx:
    # ... protected code, e.g. shared buffer access ...
    pass

# Or try without blocking.
if mtx.test():
    try:
        # ... protected code ...
        pass
    finally:
        mtx.release()

class Mutex – mutex object#

class mutex.Mutex#

Creates an unlocked mutex object.

release() None#

Unlock the mutex. Raises mutex.MutexException if the mutex is not currently locked.

test() bool#

Try to acquire the mutex in a non-blocking way. Returns True on success and False if the mutex is already locked.

To acquire the mutex in a blocking way, use the instance as a context manager (with statement). The mutex is released automatically on exit.

Exceptions#

exception mutex.MutexException#

Subclass of OSError. Raised by Mutex.release when the mutex is already unlocked.