Skip to content

Commit

Permalink
Merge pull request #19 from TEParsons/main-enh-cache-available
Browse files Browse the repository at this point in the history
ENH: Only re-scan for available devices if it's been >15s since the last scan
  • Loading branch information
TEParsons authored Dec 4, 2024
2 parents a3b2748 + 48e9a18 commit ec25a2a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion psychopy_cedrus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class BaseXidDevice(BaseDevice):
"""
Base class for all Cedrus XID devices.
"""
# used to cache results of pyxid2.getDevices as it can take a while to return
_deviceCache = {
'devices': None,
'lastChecked': 0
}

# all selectors for XID nodes
selectors = (
Expand Down Expand Up @@ -144,10 +149,19 @@ def getAvailableDevices(cls):
else:
# if missing FTDI driver, return blank rather than erroring
return []
# update cached devices if needed
if (
BaseXidDevice._deviceCache['devices'] is None
or BaseXidDevice._deviceCache['lastChecked'] < time.time() - 15
):
BaseXidDevice._deviceCache = {
'devices': pyxid2.get_xid_devices(),
'lastChecked': time.time()
}
# list devices
devices = []
# iterate through profiles of all serial port devices
for i, profile in enumerate(pyxid2.get_xid_devices()):
for i, profile in enumerate(BaseXidDevice._deviceCache['devices']):
# only include devices which match this class's product ID
if profile.product_id == cls.productId:
devices.append({
Expand Down

0 comments on commit ec25a2a

Please sign in to comment.