class documentation

class SharedLock(object):

View In Hierarchy

Implements a shared lock.

Multiple threads can acquire the lock in a shared way by calling acquire(shared=1). In order to acquire the lock in an exclusive way threads can call acquire(shared=0).

Notes on data structures: __pending contains a priority queue (heapq) of all pending acquires: [(priority1: prioqueue1), (priority2: prioqueue2), ...]. Each per-priority queue contains a normal in-order list of conditions to be notified when the lock can be acquired. Shared locks are grouped together by priority and the condition for them is stored in __pending_shared if it already exists. __pending_by_prio keeps references for the per-priority queues indexed by priority for faster access.

Method __init__ Construct a new SharedLock.
Method __repr__ Undocumented
Method acquire Acquire a shared lock.
Method delete Delete a Shared Lock.
Method downgrade Changes the lock mode from exclusive to shared.
Method GetLockInfo Retrieves information for querying locks.
Method is_owned Is the current thread somehow owning the lock at this time?
Method release Release a Shared Lock.
Class Variable __slots__ Undocumented
Instance Variable name the name of the lock
Method __acquire_unlocked Acquire a shared lock.
Method __can_acquire Determine whether lock can be acquired.
Method __check_deleted Raises an exception if the lock has been deleted.
Method __do_acquire Actually acquire the lock.
Method __find_first_pending_queue Tries to find the topmost queued entry with pending acquires.
Method __is_exclusive Is the current thread holding the lock exclusively at this time?
Method __is_on_top Checks whether the passed condition is on top of the queue.
Method __is_owned Is the current thread somehow owning the lock at this time?
Method __is_sharer Is the current thread sharing the lock at this time?
Method __notify_topmost Notifies topmost condition in queue of pending acquires.
Method _acquire_restore Undocumented
Method _check_empty Checks whether there are any pending acquires.
Method _count_pending Returns the number of pending acquires.
Method _notify_topmost Exported version of __notify_topmost.
Method _release_save Undocumented
Instance Variable __deleted Undocumented
Instance Variable __exc Undocumented
Instance Variable __lock Undocumented
Instance Variable __pending Undocumented
Instance Variable __pending_by_prio Undocumented
Instance Variable __pending_shared Undocumented
Instance Variable __shr Undocumented
Instance Variable __time_fn Undocumented
def __init__(self, name, monitor=None, _time_fn=time.time):

Construct a new SharedLock.

Parameters
namethe name of the lock
monitorLock monitor with which to register
_time_fnUndocumented
def __repr__(self):

Undocumented

def acquire(self, shared=0, timeout=None, priority=None, test_notify=None):

Acquire a shared lock.

Parameters
shared:integer (0/1) used as a booleanwhether to acquire in shared mode; by default an exclusive lock will be acquired
timeout:floatmaximum waiting time before giving up
priority:integerPriority for acquiring lock
test_notify:callable or NoneSpecial callback function for unittesting
def delete(self, timeout=None, priority=None):

Delete a Shared Lock.

This operation will declare the lock for removal. First the lock will be acquired in exclusive mode if you don't already own it, then the lock will be put in a state where any future and pending acquire() fail.

Parameters
timeout:floatmaximum waiting time before giving up
priority:integerPriority for acquiring lock
def downgrade(self):

Changes the lock mode from exclusive to shared.

Pending acquires in shared mode on the same priority will go ahead.

def GetLockInfo(self, requested):

Retrieves information for querying locks.

Parameters
requested:setRequested information, see query.LQ_*
def is_owned(self, shared=-1):

Is the current thread somehow owning the lock at this time?

Parameters
shared
  • < 0: check for any type of ownership (default)
  • 0: check for exclusive ownership
  • > 0: check for shared ownership
def release(self):

Release a Shared Lock.

You must have acquired the lock, either in shared or in exclusive mode, before calling this function.

__slots__: list[str] =

Undocumented

name: string =

the name of the lock

def __acquire_unlocked(self, shared, timeout, priority):

Acquire a shared lock.

Parameters
sharedwhether to acquire in shared mode; by default an exclusive lock will be acquired
timeoutmaximum waiting time before giving up
priority:integerPriority for acquiring lock
def __can_acquire(self, shared):

Determine whether lock can be acquired.

def __check_deleted(self):

Raises an exception if the lock has been deleted.

def __do_acquire(self, shared):

Actually acquire the lock.

def __find_first_pending_queue(self):

Tries to find the topmost queued entry with pending acquires.

Removes empty entries while going through the list.

def __is_exclusive(self):

Is the current thread holding the lock exclusively at this time?

def __is_on_top(self, cond):

Checks whether the passed condition is on top of the queue.

The caller must make sure the queue isn't empty.

def __is_owned(self, shared=-1):

Is the current thread somehow owning the lock at this time?

This is a private version of the function, which presumes you're holding the internal lock.

def __is_sharer(self):

Is the current thread sharing the lock at this time?

def __notify_topmost(self):

Notifies topmost condition in queue of pending acquires.

def _acquire_restore(self, shared):

Undocumented

def _check_empty(self):

Checks whether there are any pending acquires.

Returns
boolUndocumented
def _count_pending(self):

Returns the number of pending acquires.

Returns
intUndocumented
def _notify_topmost(self):

Exported version of __notify_topmost.

def _release_save(self):

Undocumented

__deleted: bool =

Undocumented

__exc =

Undocumented

__lock =

Undocumented

__pending: list =

Undocumented

__pending_by_prio: dict =

Undocumented

__pending_shared: dict =

Undocumented

__shr: set =

Undocumented

__time_fn =

Undocumented