|
5 | 5 | from types import TracebackType |
6 | 6 | from typing import TYPE_CHECKING, Optional, Type |
7 | 7 |
|
8 | | -from grpc import RpcError, StatusCode, insecure_channel |
| 8 | +from grpc import RpcError, StatusCode |
9 | 9 | from ni.pythonpanel.v1.python_panel_service_pb2 import ConnectRequest, DisconnectRequest |
10 | 10 | from ni.pythonpanel.v1.python_panel_service_pb2_grpc import PythonPanelServiceStub |
| 11 | +from ni_measurement_plugin_sdk_service.grpc.channelpool import GrpcChannelPool |
11 | 12 |
|
12 | 13 | from nipanel._panel_not_found_error import PanelNotFoundError |
13 | 14 |
|
|
21 | 22 | class Panel(ABC): |
22 | 23 | """This class allows you to connect to a panel and specify values for its controls.""" |
23 | 24 |
|
| 25 | + _channel_pool: GrpcChannelPool |
24 | 26 | _stub: PythonPanelServiceStub | None |
25 | 27 | _panel_id: str |
26 | 28 | _panel_uri: str |
27 | 29 |
|
28 | | - __slots__ = ["_stub", "_panel_id", "_panel_uri", "__weakref__"] |
| 30 | + __slots__ = ["_channel_pool", "_stub", "_panel_id", "_panel_uri", "__weakref__"] |
29 | 31 |
|
30 | 32 | def __init__(self, panel_id: str, panel_uri: str) -> None: |
31 | 33 | """Initialize the panel.""" |
| 34 | + self._channel_pool = GrpcChannelPool() |
32 | 35 | self._panel_id = panel_id |
33 | 36 | self._panel_uri = panel_uri |
34 | 37 |
|
@@ -59,8 +62,8 @@ def __exit__( |
59 | 62 |
|
60 | 63 | def connect(self) -> None: |
61 | 64 | """Connect to the panel and open it.""" |
62 | | - # TODO: use the channel pool |
63 | | - channel = insecure_channel(self._resolve_service_address()) |
| 65 | + address = self._resolve_service_address() |
| 66 | + channel = self._channel_pool.get_channel(address) |
64 | 67 | self._stub = PythonPanelServiceStub(channel) |
65 | 68 | connect_request = ConnectRequest(panel_id=self._panel_id, panel_uri=self._panel_uri) |
66 | 69 |
|
@@ -88,7 +91,7 @@ def disconnect(self) -> None: |
88 | 91 | raise |
89 | 92 |
|
90 | 93 | self._stub = None |
91 | | - # TODO: channel pool cleanup? |
| 94 | + self._channel_pool.close() |
92 | 95 |
|
93 | 96 | def get_value(self, value_id: str) -> object: |
94 | 97 | """Get the value for a control on the panel. |
|
0 commit comments