ReconProxy

Contents

ReconProxy#

class pulserver.proxy.ReconProxy[source]#

Bases: object

Routes each series of an MRD stream to a reconstruction.

A client connection carries one series and is served by a thread of its own: an optional config text, the header, one acquisition per readout of the chain and a CLOSE. The header’s pulserver_design user parameter names the design of the store the series was played from. Its readout table enriches the header and every acquisition; the samples are passed on as received, demodulated to the prescribed field-of-view centre by the playout (pulserver.ir.prescribe()). The reconstruction receives the chain’s sequence description as ISMRMRD waveforms, after the header and before the first acquisition (waveforms()); they are not sent back to the client. The reconstruction plugin is the one the client’s config text names. A series that breaks these rules, or whose stream ends without its CLOSE or an acquisition flagged LAST_IN_MEASUREMENT, is refused with a text naming the reason and a CLOSE, and what the client still sends is discarded until it closes the connection.

The series is reconstructed by a local worker or, with forward, by the MRD server at that address, which is sent a config file message naming forward_config or else the series’ plugin (the client’s config text is not forwarded) and decides by its own slots and queue when the series runs. Images, DICOM and text go back to the client as they arrive, once the client’s stream has ended whole: what arrives before is held until then, and dropped with a series that is refused. The client’s close closes the reconstruction’s stream, and the reconstruction’s close closes the client. A message the proxy has no reader for ends what goes back, with a text naming its type. Once the client’s stream ends, the proxy waits for the reconstruction to close unless recon_timeout caps it.

A local series holds a slot while it runs. On a host with GPUs, found through CUDA_VISIBLE_DEVICES or nvidia-smi, each slot holds one GPU, gpu_slots slots per GPU, and the reconstruction reads it as context.device. A series that finds every slot busy, or the host’s available memory short of one reconstruction’s budget while another series of this proxy runs, is queued: its enriched stream is written to a file of queue while it arrives, its client stays connected, and the file is replayed to a worker and deleted once a slot and the memory are free.

The series of one exam share a directory under the system’s temporary directory, through which a reconstruction reads what an earlier series of the exam stored in its ExamCache. The directory is deleted once a header names another exam, or nothing has leased it for EXAM_IDLE seconds, and no series of the exam still runs on any proxy.

A series whose sequence sets EnablePmc is corrected for motion while it plays: the poses its reconstruction states are published into the pose file the scan reads, motion.buf in the scan’s directory of the store, rather than sent to the client. A forwarded series publishes the poses the server passes back.

What goes back to a client is also written to a directory under the system’s temporary directory, shared with every other proxy on this host, and removed once the client has had all of it. When the client has gone, it is kept: a client opening a series with the same measurementID is sent it, once complete, in place of a new reconstruction. Kept output nobody takes up is removed after a day. With dicom_receiver, the DICOM images of a series whose client has gone are stored to that receiver once the reconstruction ends, and the output is kept only if it did not store them all.

Parameters:
  • store (Path | str) – Design store, one directory per scan, as the design calls write it.

  • plugins (PluginPath, default=None) – Directories of reconstruction plugin files, <plugin>.py, in search order; required unless the proxy forwards.

  • slots (int, default=None) – Series reconstructed at once; derived from memory and the GPUs when None.

  • gpu_slots (int, default=1) – Series reconstructed at once on each GPU, when slots is None.

  • spares (int, default=1) – Warm worker processes waiting for a series.

  • recon_timeout (float, default=None) – Seconds a reconstruction may take after the stream ends before it is stopped and the client told so; None waits for its close. A local worker is terminated; a forwarded series’ connection is closed.

  • queue (Path | str, default=None) – Directory the queued series are written to; a temporary directory, removed on close(), when None.

  • slot_directory (Path | str, default=None) – Directory the slots are held in, shared with every other proxy on this host so they count against one set; DEFAULT_SLOT_DIRECTORY when None.

  • exam_directory (Path | str, default=None) – Directory the caches of an exam are held in, shared with every other proxy on this host so a map one series measures reaches a series on another; DEFAULT_EXAM_DIRECTORY when None. A directory the proxy creates is readable by its owner alone; one that exists is used with the permissions it has. An exam’s directory is removed when the last proxy on that exam lets go of it.

  • forward (tuple[str, int], default=None) – (host, port) of the MRD server that reconstructs every series; local workers when None.

  • forward_config (str, default=None) – Config name sent to that server instead of the reconstruction plugin.

  • dicom (bool, default=False) – Convert each image to DICOM, from the enriched header, before it is relayed, whatever reconstructed it. Needed by a client that reads DICOM alone, with a plugin that emits images.

  • save_to (Path | str, default=None) – Directory each series is kept in, as the scanner sent it, so that it can be reconstructed again offline; not kept when None. A series is written to mrd_<time>.h5.part and renamed mrd_<time>.h5 once its stream has ended.

  • idle_timeout (float, default=None) – Seconds without a client after which serve() returns, counted from the last client to leave; never when None.

  • dicom_receiver (DicomReceiver, default=None) – Where the DICOM images of a series whose client has gone are stored; they are held for the client’s return when None.

Attributes:
  • workers (WorkerPool | None) – The spares assignments are taken from; None when forwarding.

  • exams (ExamCacheManager | None) – The current exam and its directory; None when forwarding.

  • queue (Path | None) – The queue directory; None when forwarding.

Raises:

ValueError – If the proxy neither forwards nor has a plugin directory.

Methods

bind

Listen on port and return the port bound; 0 takes a free one.

close

Stop accepting clients, wait for running series and release the spares.

serve

Accept clients until stop(), close(), or nothing comes.

stop

Make serve() return within one accept poll.

Attributes

port

Port the server listens on.