ni_measurement_plugin_sdk_service.measurement.service
Framework to host measurement service.
Attributes
Classes
Proxy for the Measurement Service's context-local state. |
|
Class that supports registering and hosting a python function as a gRPC service. |
Module Contents
- ni_measurement_plugin_sdk_service.measurement.service.SupportedEnumType
- class ni_measurement_plugin_sdk_service.measurement.service.MeasurementContext[source]
Proxy for the Measurement Service’s context-local state.
- property grpc_context: grpc.ServicerContext
Get the context for the RPC.
- Return type:
- property pin_map_context: ni.measurementlink.sessionmanagement.v1.client.PinMapContext
Get the pin map context for the RPC.
- Return type:
ni.measurementlink.sessionmanagement.v1.client.PinMapContext
- add_cancel_callback(cancel_callback)[source]
Add a callback which is invoked when the RPC is canceled.
- Parameters:
cancel_callback (Callable[[], None])
- Return type:
None
- abort(code, details)[source]
Aborts the RPC.
- Parameters:
code (grpc.StatusCode)
details (str)
- Return type:
None
- reserve_session(pin_or_relay_names, timeout=0.0)[source]
Reserve a single session.
Reserve the session matching the given pins, sites, and instrument type ID and return the information needed to create or access the session.
- Parameters:
pin_or_relay_names (str | collections.abc.Iterable[str]) – One or multiple pins, pin groups, relays, or relay groups to use for the measurement.
timeout (float | None) –
Timeout in seconds.
Allowed values: 0 (non-blocking, fails immediately if resources cannot be reserved), -1 (infinite timeout), or any other positive numeric value (wait for that number of seconds)
- Returns:
A reservation object with which you can query information about the session and unreserve it.
- Return type:
ni.measurementlink.sessionmanagement.v1.client.SingleSessionReservation
- reserve_sessions(pin_or_relay_names, timeout=0.0)[source]
Reserve multiple sessions.
Reserve sessions matching the given pins, sites, and instrument type ID and return the information needed to create or access the sessions.
- Parameters:
pin_or_relay_names (str | collections.abc.Iterable[str]) – One or multiple pins, pin groups, relays, or relay groups to use for the measurement.
timeout (float | None) –
Timeout in seconds.
Allowed values: 0 (non-blocking, fails immediately if resources cannot be reserved), -1 (infinite timeout), or any other positive numeric value (wait for that number of seconds)
- Returns:
A reservation object with which you can query information about the sessions and unreserve them.
- Return type:
ni.measurementlink.sessionmanagement.v1.client.MultiSessionReservation
- class ni_measurement_plugin_sdk_service.measurement.service.MeasurementService(service_config_path, version='', ui_file_paths=[], service_class=None)[source]
Class that supports registering and hosting a python function as a gRPC service.
- Parameters:
service_config_path (pathlib.Path)
version (str)
ui_file_paths (list[pathlib.Path])
service_class (str | None)
- measurement_info: ni_measurement_plugin_sdk_service.measurement.info.MeasurementInfo
Information about the measurement performed by this service.
- service_info: ni.measurementlink.discovery.v1.client.ServiceInfo
Information about this service.
- context: MeasurementContext
Accessor for context-local state.
- property channel_pool: ni_grpc_extensions.channelpool.GrpcChannelPool
Pool of gRPC channels used by the service.
- Return type:
ni_grpc_extensions.channelpool.GrpcChannelPool
- property discovery_client: ni.measurementlink.discovery.v1.client.DiscoveryClient
Client for accessing the NI Discovery Service.
- Return type:
ni.measurementlink.discovery.v1.client.DiscoveryClient
- property configuration_parameter_list: list[Any]
List of configuration parameters.
- Return type:
list[Any]
- property grpc_service: ni_measurement_plugin_sdk_service._internal.service_manager.GrpcService | None
The gRPC service object. This is a private implementation detail.
- Return type:
ni_measurement_plugin_sdk_service._internal.service_manager.GrpcService | None
- property measure_function: Callable
Registered measurement function.
- Return type:
Callable
- property service_location: ni.measurementlink.discovery.v1.client.ServiceLocation
The location of the service on the network.
- Return type:
ni.measurementlink.discovery.v1.client.ServiceLocation
- property session_management_client: ni.measurementlink.sessionmanagement.v1.client.SessionManagementClient
Client for accessing the measurement plug-in session management service.
- Return type:
ni.measurementlink.sessionmanagement.v1.client.SessionManagementClient
- register_measurement(measurement_function)[source]
Register a function as the measurement function for a measurement service.
To declare a measurement function, use this idiom:
@measurement_service.register_measurement @measurement_service.configuration("Configuration 1", ...) @measurement_service.configuration("Configuration 2", ...) @measurement_service.output("Output 1", ...) @measurement_service.output("Output 2", ...) def measure(configuration1, configuration2): ... return (output1, output2)
See also:
configuration(),output()- Parameters:
measurement_function (_F)
- Return type:
_F
- configuration(display_name, type, default_value, *, instrument_type='', enum_type=None)[source]
Add a configuration parameter to a measurement function.
This decorator maps the measurement service’s configuration parameters to Python positional parameters. To add multiple configuration parameters to the same measurement function, use this decorator multiple times. The order of decorator calls must match the order of positional parameters.
See also:
register_measurement()- Parameters:
display_name (str) – Display name of the configuration.
type (ni_measurement_plugin_sdk_service.measurement.info.DataType) – Data type of the configuration.
default_value (Any) – Default value of the configuration.
instrument_type (str) –
Filter pins by instrument type. This is only supported when configuration type is DataType.IOResource or DataType.Pin (deprecated).
For NI instruments, use instrument type id constants defined by
ni_measurement_plugin_sdk_service.session_management, such asINSTRUMENT_TYPE_NI_DCPOWERorINSTRUMENT_TYPE_NI_DMM.For custom instruments, use the instrument type id defined in the pin map file.
enum_type (SupportedEnumType | None) – Defines the enum type associated with this configuration parameter. This is only supported when configuration type is DataType.Enum or DataType.EnumArray1D.
- Returns:
Callable that takes in Any Python Function and returns the same python function.
- Return type:
Callable[[_F], _F]
- output(display_name, type, *, enum_type=None)[source]
Add an output parameter to a measurement function.
This decorator maps the measurement service’s output parameters to the elements of the tuple returned by the measurement function. To add multiple output parameters to the same measurement function, use this decorator multiple times. The order of decorator calls must match the order of elements returned by the measurement function.
See also:
register_measurement()- Parameters:
display_name (str) – Display name of the output.
type (ni_measurement_plugin_sdk_service.measurement.info.DataType) – Data type of the output.
enum_type (SupportedEnumType | None) – Defines the enum type associated with this configuration parameter. This is only supported when configuration type is DataType.Enum or DataType.EnumArray1D.
- Returns:
Callable that takes in Any Python Function and returns the same python function.
- Return type:
Callable[[_F], _F]
- host_service()[source]
Host the registered measurement method as a gRPC measurement service.
- Returns:
Context manager that can be used with a with-statement to close the service.
- Return type:
- Raises:
Exception – If register measurement methods not available.
- close_service()[source]
Stop the gRPC measurement service.
This method stops the gRPC server, unregisters with the discovery service, and cleans up the cached discovery client and gRPC channel pool.
After calling close_service(), you may call host_service() again.
Exiting the measurement service’s runtime context automatically calls close_service().
- Return type:
None
- __enter__()[source]
Enter the runtime context related to the measurement service.
- Return type:
Self
- __exit__(exc_type, exc_val, traceback)[source]
Exit the runtime context related to the measurement service.
- Parameters:
exc_type (type[BaseException] | None)
exc_val (BaseException | None)
traceback (types.TracebackType | None)
- Return type:
Literal[False]