Skip to content

Commit

Permalink
Add a default fileno function to the BusABC class. (#877)
Browse files Browse the repository at this point in the history
Modify Notifier to handle the exception raised if the bus doesn't override it.
  • Loading branch information
hardbyte committed Aug 4, 2020
1 parent 2fa99c3 commit ac6e3ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
5 changes: 4 additions & 1 deletion can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -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")
25 changes: 14 additions & 11 deletions can/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac6e3ef

Please sign in to comment.