handle sleep in udev monitoring, fixes #35
This commit is contained in:
parent
5e68094e87
commit
ad577d22d0
|
@ -121,6 +121,7 @@ def monitor_async(callback, *device_filters):
|
||||||
|
|
||||||
|
|
||||||
def monitor(callback, *device_filters):
|
def monitor(callback, *device_filters):
|
||||||
|
def _monitor():
|
||||||
c = _Context()
|
c = _Context()
|
||||||
|
|
||||||
for device in c.list_devices(subsystem='hidraw'):
|
for device in c.list_devices(subsystem='hidraw'):
|
||||||
|
@ -132,7 +133,10 @@ def monitor(callback, *device_filters):
|
||||||
break
|
break
|
||||||
|
|
||||||
m = _Monitor.from_netlink(c)
|
m = _Monitor.from_netlink(c)
|
||||||
|
del c
|
||||||
|
|
||||||
m.filter_by(subsystem='hidraw')
|
m.filter_by(subsystem='hidraw')
|
||||||
|
try:
|
||||||
for action, device in m:
|
for action, device in m:
|
||||||
# print ('----', action, device)
|
# print ('----', action, device)
|
||||||
if action in ('add', 'remove'):
|
if action in ('add', 'remove'):
|
||||||
|
@ -141,6 +145,19 @@ def monitor(callback, *device_filters):
|
||||||
if d_info:
|
if d_info:
|
||||||
callback(action, d_info)
|
callback(action, d_info)
|
||||||
break
|
break
|
||||||
|
finally:
|
||||||
|
del m
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
_monitor()
|
||||||
|
except IOError as e:
|
||||||
|
print ("monitor IOError", e)
|
||||||
|
if e.errno == _errno.EINTR:
|
||||||
|
# raised when the computer wakes from sleep
|
||||||
|
# in this case, just restart the monitor
|
||||||
|
continue
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
def enumerate(vendor_id=None, product_id=None, interface_number=None, driver=None):
|
def enumerate(vendor_id=None, product_id=None, interface_number=None, driver=None):
|
||||||
|
|
|
@ -388,3 +388,5 @@ class Receiver(object):
|
||||||
_log.exception("open %s", path)
|
_log.exception("open %s", path)
|
||||||
if e.errno == _errno.EACCES:
|
if e.errno == _errno.EACCES:
|
||||||
raise
|
raise
|
||||||
|
except:
|
||||||
|
_log.exception("open %s", path)
|
||||||
|
|
|
@ -66,12 +66,12 @@ class ReceiverListener(_listener.EventsListener):
|
||||||
if _log.isEnabledFor(_DEBUG):
|
if _log.isEnabledFor(_DEBUG):
|
||||||
_log.debug("%s: polling status: %s", self.receiver, list(iter(self.receiver)))
|
_log.debug("%s: polling status: %s", self.receiver, list(iter(self.receiver)))
|
||||||
|
|
||||||
if self._last_tick > 0 and timestamp - self._last_tick > _POLL_TICK * 3:
|
# if self._last_tick > 0 and timestamp - self._last_tick > _POLL_TICK * 2:
|
||||||
# if we missed a couple of polls, most likely the computer went into
|
# # if we missed a couple of polls, most likely the computer went into
|
||||||
# sleep, and we have to reinitialize the receiver again
|
# # sleep, and we have to reinitialize the receiver again
|
||||||
_log.warn("%s: possible sleep detected, closing this listener", self.receiver)
|
# _log.warn("%s: possible sleep detected, closing this listener", self.receiver)
|
||||||
self.stop()
|
# self.stop()
|
||||||
return
|
# return
|
||||||
|
|
||||||
self._last_tick = timestamp
|
self._last_tick = timestamp
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue