Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter for on_error to BusABC.send_periodic #1283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
@@ -81,4 +81,5 @@ Felix Nieuwenhuizen
@fjburgos
@pkess
@felixn
@Tbruno25
@Tbruno25
@andebjor
19 changes: 16 additions & 3 deletions can/bus.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
Contains the ABC bus implementation and its documentation.
"""

from typing import cast, Any, Iterator, List, Optional, Sequence, Tuple, Union
from typing import cast, Any, Callable, Iterator, List, Optional, Sequence, Tuple, Union

import can.typechecking

@@ -181,6 +181,7 @@ def send_periodic(
period: float,
duration: Optional[float] = None,
store_task: bool = True,
on_error: Optional[Callable[[Exception], bool]] = None,
) -> can.broadcastmanager.CyclicSendTaskABC:
"""Start sending messages at a given period on this bus.
@@ -191,6 +192,9 @@ def send_periodic(
- the Bus instance is shutdown
- :meth:`BusABC.stop_all_periodic_tasks()` is called
- the task's :meth:`CyclicTask.stop()` method is called.
- an error while sending and the (optional) `on_error` callback does
not return `True`. If the callback is not specified the task is
deactivated on error.
:param msgs:
Message(s) to transmit
@@ -202,6 +206,10 @@ def send_periodic(
:param store_task:
If True (the default) the task will be attached to this Bus instance.
Disable to instead manage tasks manually.
:param on_error:
Callable that accepts an exception if any error happened on a `bus`
while sending `msgs`, it shall return either ``True`` or ``False``
depending on desired behaviour of `ThreadBasedCyclicSendTask`.
:return:
A started task instance. Note the task can be stopped (and depending on
the backend modified) by calling the task's :meth:`stop` method.
@@ -232,7 +240,7 @@ def send_periodic(
# Create a backend specific task; will be patched to a _SelfRemovingCyclicTask later
task = cast(
_SelfRemovingCyclicTask,
self._send_periodic_internal(msgs, period, duration),
self._send_periodic_internal(msgs, period, duration, on_error),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail for subclasses that reimplement _send_periodic_internal()

)

# we wrap the task's stop method to also remove it from the Bus's list of tasks
@@ -260,6 +268,7 @@ def _send_periodic_internal(
msgs: Union[Sequence[Message], Message],
period: float,
duration: Optional[float] = None,
on_error: Optional[Callable[[Exception], bool]] = None,
) -> can.broadcastmanager.CyclicSendTaskABC:
"""Default implementation of periodic message sending using threading.
@@ -272,6 +281,10 @@ def _send_periodic_internal(
:param duration:
The duration between sending each message at the given rate. If
no duration is provided, the task will continue indefinitely.
:param on_error:
Callable that accepts an exception if any error happened on a `bus`
while sending `msgs`, it shall return either ``True`` or ``False``
depending on desired behaviour of `ThreadBasedCyclicSendTask`.
:return:
A started task instance. Note the task can be stopped (and
depending on the backend modified) by calling the :meth:`stop`
@@ -283,7 +296,7 @@ def _send_periodic_internal(
threading.Lock()
)
task = ThreadBasedCyclicSendTask(
self, self._lock_send_periodic, msgs, period, duration
self, self._lock_send_periodic, msgs, period, duration, on_error
)
return task