Skip to content

Commit f2c24d7

Browse files
committed
Use mutex to ensure only one thread has access at one time. (To be improved upon in the future)
1 parent dd4910d commit f2c24d7

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

Diff for: xbee_helper/device.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import datetime
99
from time import sleep
1010
from sys import version_info
11+
from multiprocessing import Lock
1112

1213
from xbee import ZigBee as ZigBeeDevice
1314

@@ -88,6 +89,7 @@ class ZigBee(object):
8889
"""
8990
_rx_frames = {}
9091
_frame_id = 1
92+
_lock = Lock()
9193

9294
def __init__(self, ser):
9395
self._ser = ser
@@ -140,16 +142,17 @@ def _send_and_wait(self, **kwargs):
140142
"""
141143
frame_id = self.next_frame_id
142144
kwargs.update(dict(frame_id=frame_id))
143-
self._send(**kwargs)
144-
timeout = datetime.now() + const.RX_TIMEOUT
145-
while datetime.now() < timeout:
146-
try:
147-
frame = self._rx_frames.pop(frame_id)
148-
raise_if_error(frame)
149-
return frame
150-
except KeyError:
151-
sleep(0.1)
152-
continue
145+
with self._lock:
146+
self._send(**kwargs)
147+
timeout = datetime.now() + const.RX_TIMEOUT
148+
while datetime.now() < timeout:
149+
try:
150+
frame = self._rx_frames.pop(frame_id)
151+
raise_if_error(frame)
152+
return frame
153+
except KeyError:
154+
sleep(0.1)
155+
continue
153156
_LOGGER.exception(
154157
"Did not receive response within configured timeout period.")
155158
raise exceptions.ZigBeeResponseTimeout()

0 commit comments

Comments
 (0)