Skip to main content

异步生成器在 USB 总线上产生检测到的热插拔事件

项目描述

aio-usb-热插拔

aio-usb-hotplug是一个 Python 库,它提供异步生成器,在 USB 总线上产生检测到的热插拔事件。

需要 Python >= 3.7。

asyncio 和一起使用trio

安装

使用包管理器pip安装 aio-usb-hotplug.

pip install aio-usb-hotplug

aio-usb-hotplug取决于PyUSB,而后者又需要libusbopenusb。满足此要求的一种简单方法是安装 libusb-package,它为大多数平台提供预编译的二进制文件:

pip install libusb-package

aio-usb-hotplug如果libusb-package安装在当前 Python 环境中,将使用 它。

用法

转储与特定 USB 设备相关的所有热插拔事件

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)

为每个匹配 VID/PID 对的 USB 设备运行异步任务

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)

贡献

欢迎请求请求。对于重大更改,请先打开一个问题讨论您想要更改的内容。

请确保根据需要更新测试。

执照

麻省理工学院

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

aio-usb-hotplug-5.0.0.tar.gz (9.8 kB 查看哈希

已上传 source

内置分布

aio_usb_hotplug-5.0.0-py3-none-any.whl (11.7 kB 查看哈希

已上传 py3