diff --git a/can/bus.py b/can/bus.py index 90a3cd906..362eefd72 100644 --- a/can/bus.py +++ b/can/bus.py @@ -199,7 +199,7 @@ def send_periodic( Disable to instead manage tasks manually. :return: A started task instance. Note the task can be stopped (and depending on - the backend modified) by calling the :meth:`stop` method. + the backend modified) by calling the task's :meth:`stop` method. .. note:: @@ -430,3 +430,6 @@ def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]: for usage in the interface's bus constructor. """ raise NotImplementedError() + + def fileno(self) -> int: + raise NotImplementedError("fileno is not implemented using current CAN bus") diff --git a/can/notifier.py b/can/notifier.py index de2894f64..eae7363ab 100644 --- a/can/notifier.py +++ b/can/notifier.py @@ -61,23 +61,26 @@ def add_bus(self, bus: BusABC): :param bus: CAN bus instance. """ - if ( - self._loop is not None - and hasattr(bus, "fileno") - and bus.fileno() >= 0 # type: ignore - ): - # Use file descriptor to watch for messages - reader = bus.fileno() # type: ignore + reader: int = -1 + try: + reader = bus.fileno() + except NotImplementedError: + # Bus doesn't support fileno, we fall back to thread based reader + pass + + if self._loop is not None and reader >= 0: + # Use bus file descriptor to watch for messages self._loop.add_reader(reader, self._on_message_available, bus) + self._readers.append(reader) else: - reader = threading.Thread( + reader_thread = threading.Thread( target=self._rx_thread, args=(bus,), name='can.notifier for bus "{}"'.format(bus.channel_info), ) - reader.daemon = True - reader.start() - self._readers.append(reader) + reader_thread.daemon = True + reader_thread.start() + self._readers.append(reader_thread) def stop(self, timeout: float = 5): """Stop notifying Listeners when new :class:`~can.Message` objects arrive