dragon.native.semaphore

The Dragon native implementation of the classical semaphore synchronization object.

The API follows the Python Multiprocessing API of Semaphore and extends it with a get_value() method.

Classes

Semaphore

This class implements Semaphore objects on top of Dragon channels.

class Semaphore

Bases: object

This class implements Semaphore objects on top of Dragon channels. We use a Dragon native Lock to protect a channel with a single entry that is used to count the number of acquire and release calls. We extend the standard API with a get_value method to ask the Semaphore for the current value.

__init__(value: int = 1, *, m_uid: int = 4611686018427387904, bounded=False)

Create a Semaphore object that can be shared across Dragon processes.

Parameters:
  • value (int, optional) – initial value for the counter, defaults to 1

  • m_uid (int, optional) – dragon managed memory pool id to use, defaults to the standard pool.

  • bounded (bool, optional) – set if the value of the Semaphore cannot exceed its initial value, defaults to False

acquire(blocking: bool = True, timeout: float | None = None) bool

Acquire the Semaphore, decrementing the counter, blocking the other processes, if the counter is 0.

Parameters:
  • blocking (bool, optional) – block the process if the Semaphore cannot be acquired immediately, defaults to True

  • timeout (float, optional) – timeout for a block in seconds, defaults to None

release(n: int = 1) None

Release the Semaphore, incrementing the internal value by n, thus unblocking up to n processes.

Parameters:

n (int, optional) – how much to increment the internal value by, defaults to 1

get_value() int

Get the value of the internal counter without acquiring the Semaphore.