ReconProxy

Contents

ReconProxy#

class pulserver.proxy.ReconProxy[source]#

Bases: object

Routes each series of an MRD stream to a reconstruction.

One thread per client connection, which carries one series: an optional config text, the header, one acquisition per readout of the chain and a CLOSE. The header’s pulserver_design names the design of the store the series was played from; its readout table enriches the header and every acquisition, and the reconstruction receives the chain’s sequence description as a text message after the header and before the first acquisition (message()), which is not sent back to the client. The readouts arrive demodulated to the prescribed field-of-view centre by the playout (pulserver.ir.prescribe()), and their samples are passed on as received. The reconstruction plugin is the one the client’s config text names. A series that breaks this 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 enriched series is reconstructed by a local worker, or, with forward, by the MRD server at that address. Whatever reconstructs it, its images, DICOM and text go back to the client as they arrive, 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, however long it takes, unless recon_timeout caps it.

A local series holds a slot for as long as it runs. On a host with GPUs, found through CUDA_VISIBLE_DEVICES or nvidia-smi, each slot holds one, gpu_slots slots per GPU, and the reconstruction reads it as context.device. A series that finds every slot busy is queued instead: 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 frees. 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. It 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 design’s directory, 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, after it is complete, in place of a new reconstruction. Kept output nobody takes up is removed after a day.

A forwarded series is sent on as it arrives; the server’s own slots and queue determine when it is reconstructed. The server is sent a config file message naming forward_config, or else the series’ reconstruction plugin; the client’s config text itself is not forwarded.

Parameters:
  • store – Directory of designs, as the design calls write it; read only.

  • plugins – Directories of reconstruction plugin files, <plugin>.py, in search order; required unless the proxy forwards.

  • slots – Series reconstructed at once; derived from memory and the GPUs when None.

  • gpu_slots – Series reconstructed at once on each GPU, when slots is None.

  • spares – Warm worker processes waiting for a series.

  • recon_timeout – 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 – Directory the queued series are written to; a temporary directory, removed on close(), when None.

  • slot_directory – 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 – 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. An exam’s directory is removed when the last proxy on that exam lets go of it.

  • forward – (host, port) of the MRD server that reconstructs every series; local workers when None.

  • forward_config – Config name sent to that server instead of the reconstruction plugin.

  • dicom – Convert each image to DICOM, from the enriched header, before it is relayed. Whatever reconstructed it: a client that reads DICOM alone, such as a scanner’s, needs this of a plugin that emits images.

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.