Inefficient Disk Detection #14

Open
opened 2026-02-02 14:48:43 -05:00 by jared · 0 comments
Owner

_get_all_disks() calls glob.glob() multiple times and uses sets inefficiently:

Current approach scans filesystem multiple times

for pattern in ['/dev/sd[a-z]', '/dev/nvme[0-9]n[0-9]']:
matches = glob.glob(pattern)

Optimize:

def _get_all_disks(self) -> List[str]:
"""Get all physical disks efficiently."""
disks = set()

# Single lsblk call gets everything
try:
    result = subprocess.run(
        ['lsblk', '-d', '-n', '-o', 'NAME,TYPE', '-p'],  # Add -p for full paths
        stdout=subprocess.PIPE, text=True, timeout=10
    )
    for line in result.stdout.strip().split('\n'):
        if line:
            parts = line.split()
            if len(parts) >= 2 and parts[1] == 'disk':
                disks.add(parts[0])  # Already has /dev/ prefix
    return sorted(disks)  # Return sorted for consistency
except Exception as e:
    logger.error(f"Failed to detect disks: {e}")
    return []
_get_all_disks() calls glob.glob() multiple times and uses sets inefficiently: # Current approach scans filesystem multiple times for pattern in ['/dev/sd[a-z]', '/dev/nvme[0-9]n[0-9]']: matches = glob.glob(pattern) Optimize: def _get_all_disks(self) -> List[str]: """Get all physical disks efficiently.""" disks = set() # Single lsblk call gets everything try: result = subprocess.run( ['lsblk', '-d', '-n', '-o', 'NAME,TYPE', '-p'], # Add -p for full paths stdout=subprocess.PIPE, text=True, timeout=10 ) for line in result.stdout.strip().split('\n'): if line: parts = line.split() if len(parts) >= 2 and parts[1] == 'disk': disks.add(parts[0]) # Already has /dev/ prefix return sorted(disks) # Return sorted for consistency except Exception as e: logger.error(f"Failed to detect disks: {e}") return []
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: LotusGuild/hwmonDaemon#14