Skip to content

fix: discover_realsense() re-runs ~2s USB enumeration on every call #166

Description

@AlbertvanHouten

Summary

physicalai.capture.discover_all() is slow (>2s per call) because discover_realsense() creates and discards a pyrealsense2 context on every invocation. pyrealsense2 re-runs a full cold USB enumeration (~2s) whenever no rs.context() is alive in the process. Holding a single context alive for the process lifetime makes every discovery instant.

Steps to reproduce

physicalai/capture/cameras/realsense/_discover.py:

def discover_realsense() -> list[DeviceInfo]:
    ...
    ctx = rs_any.context()          # local context
    for i, dev in enumerate(ctx.query_devices()):
        ...
    return results                  # ctx is GC'd here → next call pays ~2s again

The context is local, so it's garbage-collected on return. pyrealsense2 tears down its USB enumeration state when the last context dies and re-pays the cold-enumeration cost (~2s) on the next context()/query_devices().

Logs and traceback

Environment

physicalai==0.1.1
macOS, pyrealsense2 installed, no RealSense device connected
Reproducible with zero cameras attached

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions