Skip to content

Commit 200598f

Browse files
committed
lxaiobus: import requests on-demand
This matches the approach we have in other drivers already and improves the startup time of labgrid-client by ~11%. Signed-off-by: Jan Luebbe <jlu@pengutronix.de>
1 parent 11609b7 commit 200598f

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

labgrid/driver/lxaiobusdriver.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import attr
1+
from importlib import import_module
22

3-
import requests
3+
import attr
44

55
from ..factory import target_factory
66
from ..protocol import DigitalOutputProtocol
@@ -18,6 +18,7 @@ class LXAIOBusPIODriver(Driver, DigitalOutputProtocol):
1818

1919
def __attrs_post_init__(self):
2020
super().__attrs_post_init__()
21+
self._requests = import_module('requests')
2122

2223
def on_activate(self):
2324
# we can only forward if the backend knows which port to use
@@ -29,7 +30,7 @@ def on_activate(self):
2930
def set(self, status):
3031
if self.pio.invert:
3132
status = not status
32-
r = requests.post(
33+
r = self._requests.post(
3334
self._url, data={'value': '1' if status else '0'}
3435
)
3536
r.raise_for_status()
@@ -40,7 +41,7 @@ def set(self, status):
4041
@Driver.check_active
4142
@step(result=['True'])
4243
def get(self):
43-
r = requests.get(self._url)
44+
r = self._requests.get(self._url)
4445
r.raise_for_status()
4546
j = r.json()
4647
if j["code"] != 0:

labgrid/resource/lxaiobus.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import logging
22
from time import monotonic
3+
from importlib import import_module
34

45
import attr
5-
import requests
66

77
from ..factory import target_factory
88
from .common import ManagedResource, ResourceManager
@@ -12,18 +12,19 @@
1212
class LXAIOBusNodeManager(ResourceManager):
1313
def __attrs_post_init__(self):
1414
super().__attrs_post_init__()
15+
self._requests = import_module('requests')
1516

1617
self.log = logging.getLogger('LXAIOBusNodeManager')
1718

1819
self._last = 0.0
1920

2021
def _get_nodes(self, host):
2122
try:
22-
r = requests.get(f'http://{host}/nodes/')
23+
r = self._requests.get(f'http://{host}/nodes/')
2324
r.raise_for_status()
2425
j = r.json()
2526
return j["result"]
26-
except requests.exceptions.ConnectionError:
27+
except self._requests.exceptions.ConnectionError:
2728
self.log.exception("failed to connect to host %s", host)
2829
return []
2930

0 commit comments

Comments
 (0)