fictive.patterns.concurrency module

Concurrency Primitives

Classes

ContextLock

provides some context-management features for an encapsulated lock

SpinLock

Implements a spin lock around a nonblocking lock

class fictive.patterns.concurrency.ContextLock(lock)[source]

Bases: object

provides some context-management features for an encapsulated lock

exception LockNotAcquiredError[source]

Bases: Exception

raised by a protected function when the lock could not be acquired

__init__(lock)[source]

Initialize self. See help(type(self)) for accurate signature.

acquire(blocking=True, timeout=- 1)[source]

acquire the lock

acquire_context(blocking=True, timeout=- 1)[source]

a context manager that will attempt to acquire the lock

NB: if blocking is False or timeout is > 0, make sure to check the returned value to determine whteher the lock was actually acquired. If a LockNotAcquiredError is raised within the context, it will be handled by the context manager and the context will immediately cleanup and exit:

lock = SpinLock(some_non_blocking_lock)
with lock.acquire_context(timeout=5) as acquired:
    if not aquired:
        raise lock.LockNotAcquiredError()
    # lock is acquired
    do_something()
property protect

returns a decorator that will acquire the lock before invoking the wrapped function

release()[source]

release the lock

class fictive.patterns.concurrency.SpinLock(lock, spin_delay=None)[source]

Bases: object

Implements a spin lock around a nonblocking lock

DEFAULT_SPIN_DELAY = 0.001
__init__(lock, spin_delay=None)[source]

Initialize self. See help(type(self)) for accurate signature.

acquire(blocking=True, timeout=- 1)[source]

If blocking, keep trying the lock until acquired (or timeout is reached)

release()[source]

release the lock