ducks.concurrent package

Submodules

ducks.concurrent.main module

class ducks.concurrent.main.ConcurrentDex(objs: Iterable[Any] | None = None, on: Iterable[str | Callable] | None = None, priority: str = 'readers')

Bases: object

__getitem__(query: Dict) List[Any]

Get a read lock and perform Dex __getitem__.

__init__(objs: Iterable[Any] | None = None, on: Iterable[str | Callable] | None = None, priority: str = 'readers')

Contains a Dex instance and a readerwriterlock. Wraps each Dex method in a read or write lock.

Parameters:
  • objs – see Dex API

  • on – see Dex API

  • priority – ‘readers’, ‘writers’, or ‘fair’. Default ‘readers’. Change this according to your usage pattern.

add(obj: Any)

Get a write lock and perform Dex.add().

get_values(attr: str | Callable)

Get a read lock and perform Dex get_values().

read_lock()

Lock the ConcurrentDex for reading.

remove(obj: Any)

Get a write lock and perform Dex.remove().

update(obj: Any)

Get a write lock and perform Dex.update().

write_lock()

Lock the ConcurrentDex for writing.

When doing many write operations at once, it is more efficient to do::
with cfb.read_lock():
for item in items:

cfb.box.add(item) # calls add() on the underlying Dex.

This performs locking only once, versus calling cfb.add() which locks for each item. The same pattern works for update() and remove().

ducks.concurrent.main.load(saved: Dict) ConcurrentDex

Creates a ConcurrentDex from the pickle file contents.

ducks.concurrent.main.save(c_box: ConcurrentDex, filepath: str)

Saves a ConcurrentDex to a pickle file.

Module contents