Python evdev检测设备已拔掉

我正在使用伟大的“evdev”库来收听USB条形码阅读器输入,我需要检测设备是否突然被拔出/无响应,否则读取循环的
python脚本会在单个线程上达到100%cpu使用率慢慢开始吃掉所有可用的内存,这会导致整个系统崩溃.

我们的想法是检测设备何时拔出并杀死当前脚本,导致主管尝试重新启动它,直到设备重新插入/变为响应状态.

我用来读取输入的代码如下:

devices = map(InputDevice, list_devices())

keys = {
    2: 1,
    3: 2,
    4: 3,
    5: 4,
    6: 5,
    7: 6,
    8: 7,
    9: 8,
    10: 9,
    11: 0,
}
dev = None
for d in devices:
    if d.name == 'Symbol Technologies, Inc, 2008 Symbol Bar Code Scanner':
        print('%-20s %-32s %s' % (d.fn, d.name, d.phys))
        dev = InputDevice(d.fn)
        break

if dev is not None:
    code = []
    for event in dev.read_loop():
        if event.type == ecodes.EV_KEY:
            if event.value == 00:
                if event.code != 96:
                    try:
                        code.append(keys[event.code])
                    except:
                        code.append('-')
                else:
                    card = "".join(map(str, code))
                    print card

                    code = []
                    card = ""

那么我该如何以正确的方式去做呢?
我可能会工作的一种方法是第二个脚本,每隔1-5分钟从cron运行一次,检查所述设备是否仍然可用,如果现在是这样,从某个文件中获取进程ID并以此方式终止进程,但问题是这个方法是,如果设备被拔掉,然后在检查之间插回,“检查器”脚本认为一切正常,而主脚本正在慢慢崩溃 – 它在“拔出”之后不会重新激活

最佳答案
python-evdev作者在这里.知道一个人的工作对别人有用,这是一种很好的感觉.谢谢你!

你一定要看看linux的设备管理器 – udev.每当添加或删除设备时,linux内核都会发出事件.要在Python程序中侦听这些事件,可以使用pyudev,这是一个非常好的,基于ctypes的绑定到libudev(参见monitoring部分).

以下是使用evdev和pyudev的示例:

import functools
import pyudev

from evdev import InputDevice
from select import select

context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='input')
monitor.start()

fds = {monitor.fileno(): monitor}
finalizers = []

while True:
    r, w, x = select(fds, [], [])

    if monitor.fileno() in r:
        r.remove(monitor.fileno())

        for udev in iter(functools.partial(monitor.poll, 0), None):
            # we're only interested in devices that have a device node
            # (e.g. /dev/input/eventX)
            if not udev.device_node:
                break

            # find the device we're interested in and add it to fds
            for name in (i['NAME'] for i in udev.ancestors if 'NAME' in i):
                # I used a virtual input device for this test - you
                # should adapt this to your needs
                if u'py-evdev-uinput' in name:
                    if udev.action == u'add':
                        print('Device added: %s' % udev)
                        fds[dev.fd] = InputDevice(udev.device_node)
                        break
                    if udev.action == u'remove':
                        print('Device removed: %s' % udev)
                        def helper():
                            global fds
                            fds = {monitor.fileno(): monitor}
                        finalizers.append(helper)
                        break

    for fd in r:
        dev = fds[fd]
        for event in dev.read():
            print(event)

    for i in range(len(finalizers)):
        finalizers.pop()()
点赞