Skip to content

Commit 2a70f19

Browse files
committed
use more efficient synchronization when waiting for responses
1 parent c6170db commit 2a70f19

4 files changed

Lines changed: 52 additions & 46 deletions

File tree

poetry.lock

Lines changed: 36 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scpi/transports/baseclass.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import asyncio
66
import logging
7+
import threading
78

89
logger = logging.getLogger(__name__)
910

@@ -19,6 +20,8 @@ class BaseTransport(AbstractTransport):
1920
message_callback = None
2021
unsolicited_message_callback = None
2122
lock = asyncio.Lock()
23+
aioevent = asyncio.Event()
24+
blevent = threading.Event()
2225

2326
async def quit(self):
2427
"""Must shutdown all background threads (if any)"""

src/scpi/transports/gpib/prologix.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
""""Driver" for http://prologix.biz/gpib-usb-controller.html GPIB controller"""
22
import asyncio
3+
import logging
4+
import threading
35

46
import serial
57
import serial.threaded
@@ -10,6 +12,7 @@
1012

1113
SCAN_DEVICE_TIMEOUT = 0.5
1214
READ_TIMEOUT = 1.0
15+
LOGGER = logging.getLogger(__name__)
1316

1417

1518
class PrologixRS232SerialProtocol(RS232SerialProtocol):
@@ -67,13 +70,14 @@ async def send_and_read(self, send):
6770

6871
def set_response(message):
6972
"""Callback for setting the response"""
70-
nonlocal response
73+
nonlocal response, self
7174
response = message
75+
self.blevent.set()
7276

77+
self.blevent.clear()
7378
self.message_callback = set_response
7479
self.serialhandler.protocol.write_line(send)
75-
while response is None:
76-
await asyncio.sleep(0)
80+
await asyncio.get_event_loop().run_in_executor(None, self.blevent.wait)
7781
return response
7882

7983
async def set_address(self, address, secondary=None):
@@ -82,9 +86,9 @@ async def set_address(self, address, secondary=None):
8286
await self.send_command("++addr %d" % address)
8387
else:
8488
await self.send_command("++addr %d %d" % (address, secondary))
85-
# Wait for the address to actually be set
89+
8690
while True:
87-
await asyncio.sleep(0)
91+
await asyncio.sleep(0.001)
8892
resp = await self.query_address()
8993
if resp == (address, secondary):
9094
break

src/scpi/transports/rs232.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ async def get_response(self):
5151

5252
def set_response(message):
5353
"""Callback for setting the response"""
54-
nonlocal response
54+
nonlocal response, self
5555
response = message
56+
self.blevent.set()
5657

58+
self.blevent.clear()
5759
self.message_callback = set_response
58-
while response is None:
59-
await asyncio.sleep(0)
60+
await asyncio.get_event_loop().run_in_executor(None, self.blevent.wait)
6061
return response
6162

6263
async def abort_command(self):

0 commit comments

Comments
 (0)