def poll(self, timeout=None): """ Poll for a device event. You can use this method together with :func:`iter()` to synchronously monitor events in the current thread:: for device in iter(monitor.poll, None): print('{0.action} on {0.device_path}'.format(device)) Since this method will never return ``None`` if no ``timeout`` is specified, this is effectively an endless loop. With :func:`functools.partial()` you can also create a loop that only waits for a specified time:: for device in iter(partial(monitor.poll, 3), None): print('{0.action} on {0.device_path}'.format(device)) This loop will only wait three seconds for a new device event. If no device event occurred after three seconds, the loop will exit. ``timeout`` is a floating point number that specifies a time-out in seconds. If omitted or ``None``, this method blocks until a device event is available. If ``0``, this method just polls and will never block. .. note:: This method implicitly calls :meth:`start()`. Return the received :class:`Device`, or ``None`` if a timeout occurred. Raise :exc:`~exceptions.EnvironmentError` if event retrieval failed. .. seealso:: :attr:`Device.action` The action that created this event. :attr:`Device.sequence_number` The sequence number of this event. .. versionadded:: 0.16 """ if timeout is not None and timeout > 0: # .poll() takes timeout in milliseconds timeout = int(timeout * 1000) self.start() if eintr_retry_call(Poll.for_events((self, 'r')).poll, timeout): return self._receive_device() else: return None
def poll(self, timeout=None): """ Poll for a device event. You can use this method together with :func:`iter()` to synchronously monitor events in the current thread:: for device in iter(monitor.poll, None): print('{0.action} on {0.device_path}'.format(device)) Since this method will never return ``None`` if no ``timeout`` is specified, this is effectively an endless loop. With :func:`functools.partial()` you can also create a loop that only waits for a specified time:: for device in iter(partial(monitor.poll, 3), None): print('{0.action} on {0.device_path}'.format(device)) This loop will only wait three seconds for a new device event. If no device event occurred after three seconds, the loop will exit. ``timeout`` is a floating point number that specifies a time-out in seconds. If omitted or ``None``, this method blocks until a device event is available. If ``0``, this method just polls and will never block. .. note:: This method implicitly calls :meth:`start()`. Return the received :class:`Device`, or ``None`` if a timeout occurred. Raise :exc:`~exceptions.EnvironmentError` if event retrieval failed. .. seealso:: :attr:`Device.action` The action that created this event. :attr:`Device.sequence_number` The sequence number of this event. .. versionadded:: 0.16 """ if timeout is not None and timeout > 0: # .poll() takes timeout in milliseconds timeout = int(timeout * 1000) self.start() if Poll.for_events((self, 'r')).poll(timeout): return self._receive_device() else: return None
def run(self): self.monitor.start() notifier = Poll.for_events( (self.monitor, 'r'), (self._stop_event.source, 'r')) while True: for fd, event in notifier.poll(): if fd == self._stop_event.source.fileno(): # in case of a stop event, close our pipe side, and # return from the thread self._stop_event.source.close() return elif fd == self.monitor.fileno() and event == 'r': read_device = partial(self.monitor.poll, timeout=0) for device in iter(read_device, None): self._callback(device) else: raise EnvironmentError('Observed monitor hung up')
def run(self): self.monitor.start() notifier = Poll.for_events( (self.monitor, 'r'), (self._stop_event.source, 'r')) while True: for file_descriptor, event in eintr_retry_call(notifier.poll): if file_descriptor == self._stop_event.source.fileno(): # in case of a stop event, close our pipe side, and # return from the thread self._stop_event.source.close() return elif file_descriptor == self.monitor.fileno() and event == 'r': read_device = partial(eintr_retry_call, self.monitor.poll, timeout=0) for device in iter(read_device, None): self._callback(device) else: raise EnvironmentError('Observed monitor hung up')