Python evdev检测设备已拔掉
发布时间:2020-12-20 11:26:39 所属栏目:Python 来源:网络整理
导读:我正在使用伟大的“evdev”库来收听USB条形码阅读器输入,我需要检测设备是否突然被拔出/无响应,否则读取循环的 python脚本会在单个线程上达到100%cpu使用率慢慢开始吃掉所有可用的内存,这会导致整个系统崩溃. 我们的想法是检测设备何时拔出并杀死当前脚本,
我正在使用伟大的“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 = "" 那么我该如何以正确的方式去做呢? 解决方法
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()() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |