xopto.mcbase.mcprogress module

class ProgressMonitor(mcsim: xopto.mcbase.mcobject.McObject, interval: float = 0.5, cb: Optional[Callable[[xopto.mcbase.mcprogress.ProgressMonitor], None]] = None, cbargs: Optional[Tuple] = None, cbkwargs: Optional[Dict] = None)[source]

Bases: object

Initialize Monte Carlo progress monitor.

Parameters
  • mcsim (mcobject.McObject) – Monte Carlo simulator instance.

  • target (int) – Optional target/final value for the monitored quantity, e.g. the number of launched packets.

  • interval (float) – Polling interval in seconds.

  • cb (Callable[[ProgressMonitor], None]) – A callable that is executed when the progress state changes. The first argument of the callback is the ProgressMonitor instance followed by the optional positional arguments (cbargs) and keyword arguments (cbkwargs).

  • cbargs (list or tuple) – Optional positional arguments for the callback.

  • cbkwargs (dict) – Optional keyword arguments for the callback.

Note

The callback functionality is provided for basic usage. The preferred way of implementing custom progress monitors is to subclass the ProgressMonitor and overload the update() method.

processed()int[source]

Returns the number of processed items.

Returns

processed – The number of processed items.

Return type

int

progress()float[source]

Returns progress as a floating point number from 0.0 to 1.0, where 1.0 is returned when the monitored task is complete.

Returns

progress – Returns a value between 0.0 and 1.0 (complete).

Return type

float

resume(target: Optional[int] = None)[source]

Resume the monitor with the last state.

Parameters

target (int) – Optional new target/final value for the monitored quantity, e.g. the number of launched packets, that will replace the existing target value.

start(target: int, terminate: bool = True)xopto.mcbase.mcprogress.ProgressMonitor[source]

Starts monitoring the progress of the related Monte Carlo simulator instance.

Parameters
  • target (int) – Optional target/final value for the monitored quantity, e.g. the number of launched packets.

  • terminate (bool) – Flag that terminates the monitor if set to True. Setting this flag to False will allow to reuse the monitor. When using the monitor in a for loop, it is much more efficient to reuse the monitor, since each instance of a monitor is using a background thread that needs to be created and initialized before the monitoring can star.

Returns

self – Returns self.

Return type

ProgressMonitor

Note

Note that a terminated monitor cannot be restarted. A RuntimeError will be raised if an attept is made to start a terminated monitor.

stop()[source]

Stop monitoring the progres of the related Monte Carlo simulator instance. Note that the monitoring can be restarted.

target()int[source]

Returns the current target value of the monitored quantity.

Returns

target – Target/final value for the monitored quantity, e.g the number of launched packets.

Return type

int

terminate()[source]

Terminate the progress monitor. Note that a terminated progress monitor cannot be restarted.

threads()int[source]

Returns the number of OpenCL threads that are working on the job. Returns None, if this functionality is not supported.

Returns

n – the number of OpenCL threads that are working on the job or None if not supported.

Return type

int

update()[source]

Overload this method for custom handling of the progress. This function is called each time the state of the progress changes. Note that the polling interval is set with the constructor __init__() parameter interval.

Note

Note that this method is called in the context of the background thread that periodically communicates/polls the OpenCL device.