ExamCache

Contents

ExamCache#

class pulserver.recon.ExamCache[source]#

Bases: MutableMapping[Hashable, Any]

Thread-safe store of artifacts shared by the series of one exam.

A value is disposed when replaced by another object, deleted, cleared, or when the cache closes: through the cleanup stored with it, else its close() method when it has one. Disposal errors are logged, not raised. Reads and writes after close() raise RuntimeError.

With a directory, every value stored is also written there with pickle, and a key this cache does not hold is read from there, so the caches of one exam in different processes share their values. A value read back is a copy, without the cleanup stored with it; a key or value that cannot be pickled is held by this cache alone. Closing leaves the directory as it is.

Parameters:
  • exam_id – Identifier of the owning exam.

  • directory – Directory the exam’s caches share; None holds values in memory only. Its files are unpickled, so only the processes sharing the exam may write to it: the proxy creates it under a directory only its own user can open.

Examples

>>> import pulserver.recon as recon
>>> cache = recon.ExamCache("exam-1")
>>> cache["coil_maps"] = "maps"
>>> "coil_maps" in cache, len(cache)
(True, 1)

Methods

clear

Dispose and remove every cached artifact, the directory's included.

close

Dispose every artifact and retire the cache; repeated calls do nothing.

get

get_or_create

Return the artifact at key, calling factory to create it when absent.

items

keys

pop

Remove and return an artifact without disposing it.

popitem

as a 2-tuple; but raise KeyError if D is empty.

set

Store an artifact and return it, disposing the value it replaces.

setdefault

update

If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values

Attributes

closed

Whether close() has run.