Python evdev检测设备已拔掉

Python evdev检测设备已拔掉,第1张

概述我正在使用伟大的“evdev”库来收听USB条形码阅读器输入,我需要检测设备是否突然被拔出/无响应,否则读取循环的 python脚本会在单个线程上达到100%cpu使用率慢慢开始吃掉所有可用的内存,这会导致整个系统崩溃. 我们的想法是检测设备何时拔出并杀死当前脚本,导致主管尝试重新启动它,直到设备重新插入/变为响应状态. 我用来读取输入的代码如下: devices = map(InputDevic 我正在使用伟大的“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 = Nonefor 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)        breakif 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的示例:

@R_403_5565@ functools@R_403_5565@ pyudevfrom evdev @R_403_5565@ inputDevicefrom select @R_403_5565@ selectcontext = 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()()
总结

以上是内存溢出为你收集整理的Python evdev检测设备已拔掉全部内容,希望文章能够帮你解决Python evdev检测设备已拔掉所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1192797.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存