Skip to content

Commit 8fd691e

Browse files
authored
MeasurementService: Use a context manager to simplify ctrl-C handling (#110)
1 parent 53e0863 commit 8fd691e

9 files changed

Lines changed: 29 additions & 48 deletions

File tree

examples/daqmx_analog_input_measurement/measurement.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ def main(verbose: int):
8888
level = logging.WARNING
8989
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=level)
9090

91-
daqmx_analog_input_measurement_service.host_service()
92-
input("Press enter to close the measurement service.\n")
93-
daqmx_analog_input_measurement_service.close_service()
91+
with daqmx_analog_input_measurement_service.host_service():
92+
input("Press enter to close the measurement service.\n")
9493

9594

9695
if __name__ == "__main__":

examples/dc_measurement_labviewUI/measurement.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ def main(verbose: int):
142142
level = logging.WARNING
143143
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=level)
144144

145-
dc_measurement_service.host_service()
146-
input("Press enter to close the measurement service.\n")
147-
dc_measurement_service.close_service()
145+
with dc_measurement_service.host_service():
146+
input("Press enter to close the measurement service.\n")
148147

149148

150149
if __name__ == "__main__":

examples/dc_measurement_measurementUI/measurement.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ def main(verbose: int):
142142
level = logging.WARNING
143143
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=level)
144144

145-
dc_measurement_service.host_service()
146-
input("Press enter to close the measurement service.\n")
147-
dc_measurement_service.close_service()
145+
with dc_measurement_service.host_service():
146+
input("Press enter to close the measurement service.\n")
148147

149148

150149
if __name__ == "__main__":

examples/sample_measurement/measurement.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ def main(verbose: int):
7171
level = logging.WARNING
7272
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=level)
7373

74-
sample_measurement_service.host_service()
75-
input("Press enter to close the measurement service.\n")
76-
sample_measurement_service.close_service()
74+
with sample_measurement_service.host_service():
75+
input("Press enter to close the measurement service.\n")
7776

7877

7978
if __name__ == "__main__":

ni_measurement_generator/ni_measurement_generator/templates/measurement.py.mako

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ def main(verbose: int):
4848
level = logging.WARNING
4949
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=level)
5050

51-
measurement_service.host_service()
52-
input("Press enter to close the measurement service.\n")
53-
measurement_service.close_service()
51+
with measurement_service.host_service():
52+
input("Press enter to close the measurement service.\n")
5453

5554

5655
if __name__ == "__main__":

ni_measurement_generator/tests/test_assets/example_renders/example.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ def main(verbose: int):
4646
level = logging.WARNING
4747
logging.basicConfig(format="%(asctime)s %(levelname)s: %(message)s", level=level)
4848

49-
measurement_service.host_service()
50-
input("Press enter to close the measurement service.\n")
51-
measurement_service.close_service()
49+
with measurement_service.host_service():
50+
input("Press enter to close the measurement service.\n")
5251

5352

5453
if __name__ == "__main__":

ni_measurement_service/_internal/service_manager.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from ni_measurement_service._internal.stubs.ni.measurements.measurementservice import (
1111
measurement_service_pb2_grpc,
1212
)
13-
from ni_measurement_service._internal.utilities import console_exit_functions
1413
from ni_measurement_service.measurement.info import MeasurementInfo, ServiceInfo
1514

1615

@@ -91,7 +90,6 @@ def start(
9190
_logger.info("Measurement service hosted on port: %s", port)
9291
self.discovery_client.register_measurement_service(port, service_info, measurement_info)
9392

94-
console_exit_functions.setup_unregister_on_console_close(self.stop)
9593
self.port = port
9694
return port
9795

ni_measurement_service/_internal/utilities/console_exit_functions.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

ni_measurement_service/measurement/service.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Framework to host measurement service."""
22

3+
from __future__ import annotations
4+
35
from typing import Any, Callable
46

57
from ni_measurement_service._internal import grpc_servicer
@@ -143,9 +145,14 @@ def _output(func):
143145

144146
return _output
145147

146-
def host_service(self) -> None:
148+
def host_service(self) -> MeasurementService:
147149
"""Host the registered measurement method as gRPC measurement service.
148150
151+
Returns
152+
-------
153+
MeasurementService: Context manager that can be used with a with-statement to close
154+
the service.
155+
149156
Raises
150157
------
151158
Exception: If register measurement methods not available.
@@ -160,8 +167,16 @@ def host_service(self) -> None:
160167
self.output_parameter_list,
161168
self.measure_function,
162169
)
163-
return None
170+
return self
164171

165172
def close_service(self) -> None:
166173
"""Close the Service after un-registering with discovery service and cleanups."""
167174
self.grpc_service.stop()
175+
176+
def __enter__(self) -> MeasurementService:
177+
"""Enter the runtime context related to the measurement service."""
178+
return self
179+
180+
def __exit__(self, exc_type, exc_value, traceback) -> None:
181+
"""Exit the runtime context related to the measurement service."""
182+
self.close_service()

0 commit comments

Comments
 (0)