aio-usb-hotplug
is a Python library that provides asynchronous generators
yielding detected hotplug events on the USB buses.
Requires Python >= 3.8.
Use the package manager pip to install
aio-usb-hotplug
.
pip install aio-usb-hotplug
aio-usb-hotplug
depends on PyUSB, which
in turn requires libusb or
openusb. An easy way to satisfy
this requirement is to install
libusb-package, which supplies
pre-compiled binaries for most platforms:
pip install libusb-package
aio-usb-hotplug
will make use of
libusb-package if it is installed in
the current Python environment.
from aio_usb_hotplug import HotplugDetector
from trio import run # ...or asyncio
async def dump_events():
detector = HotplugDetector.for_device(vid="1050", pid="0407")
async for event in detector.events():
print(repr(event))
trio.run(dump_events)
from aio_usb_hotplug import HotplugDetector
from trio import sleep_forever
async def handle_device(device):
print("Handling device:", repr(device))
try:
# Do something meaningful with the device. The task gets cancelled
# when the device is unplugged.
await sleep_forever()
finally:
# Device unplugged or an exception happened
print("Stopped handling device:", repr(device))
async def handle_detected_devices():
detector = HotplugDetector.for_device(vid="1050", pid="0407")
await detector.run_for_each_device(handle_device)
trio.run(handle_detected_devices)
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.