我使用强大的"evdev“库来监听USB条形码阅读器的输入,我需要检测设备是否突然拔出/没有响应,否则读取循环的python脚本会在单个线程上达到100%的cpu使用率,并慢慢开始消耗所有可用内存,这会导致整个系统在一段时间后崩溃。
其思路是检测设备何时拔出,并终止导致supervisor尝试重新启动它的当前脚本,直到设备重新插入/开始响应。
我用来读取输入的代码如下:
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并以这种方式终止进程。但是这种方法的问题是,如果设备被拔出,然后在检查之间插入回来,“检查器”脚本认为一切正常,而主脚本正在慢慢崩溃-它不会在“拔出”之后重新激活。
发布于 2013-05-12 02:35:42
python-evdev的作者在这里。知道自己的工作对别人有用是一种很棒的感觉。谢谢你这么做!
你一定要看看linux的设备管理器-- udev。每当添加或删除设备时,linux内核都会发出事件。要在Python程序中监听这些事件,可以使用pyudev,它是一个优秀的、基于ctype的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()()https://stackoverflow.com/questions/15944987
复制相似问题