Skip to content

Commit

Permalink
Add autostart option (kwarg) to BusABC.send_periodic() to fix issue #…
Browse files Browse the repository at this point in the history
  • Loading branch information
SWolfSchunk committed Sep 6, 2024
1 parent c712131 commit 56262ea
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion can/broadcastmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def __init__(
period: float,
duration: Optional[float] = None,
on_error: Optional[Callable[[Exception], bool]] = None,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> None:
"""Transmits `messages` with a `period` seconds for `duration` seconds on a `bus`.
Expand Down Expand Up @@ -324,7 +325,8 @@ def __init__(
stacklevel=1,
)

self.start()
if autostart:
self.start()

def stop(self) -> None:
self.stopped = True
Expand Down
11 changes: 10 additions & 1 deletion can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def send_periodic(
period: float,
duration: Optional[float] = None,
store_task: bool = True,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> can.broadcastmanager.CyclicSendTaskABC:
"""Start sending messages at a given period on this bus.
Expand All @@ -237,6 +238,9 @@ 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 autostart:
If True (the default) the sending task will immediately start after creation.
Otherwise, the task has to be started by calling start() on it.
:param modifier_callback:
Function which should be used to modify each message's data before
sending. The callback modifies the :attr:`~can.Message.data` of the
Expand Down Expand Up @@ -272,7 +276,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, modifier_callback),
self._send_periodic_internal(msgs, period, duration, autostart, modifier_callback),
)
# we wrap the task's stop method to also remove it from the Bus's list of tasks
periodic_tasks = self._periodic_tasks
Expand All @@ -299,6 +303,7 @@ def _send_periodic_internal(
msgs: Union[Sequence[Message], Message],
period: float,
duration: Optional[float] = None,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> can.broadcastmanager.CyclicSendTaskABC:
"""Default implementation of periodic message sending using threading.
Expand All @@ -312,6 +317,9 @@ 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 autostart:
If True (the default) the sending task will immediately start after creation.
Otherwise, the task has to be started by calling start() on it.
:return:
A started task instance. Note the task can be stopped (and
depending on the backend modified) by calling the
Expand All @@ -328,6 +336,7 @@ def _send_periodic_internal(
messages=msgs,
period=period,
duration=duration,
autostart=autostart,
modifier_callback=modifier_callback,
)
return task
Expand Down
3 changes: 2 additions & 1 deletion can/interfaces/ixxat/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ def _send_periodic_internal(
msgs: Union[Sequence[Message], Message],
period: float,
duration: Optional[float] = None,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> CyclicSendTaskABC:
return self.bus._send_periodic_internal(
msgs, period, duration, modifier_callback
msgs, period, duration, autostart, modifier_callback
)

def shutdown(self) -> None:
Expand Down
2 changes: 2 additions & 0 deletions can/interfaces/ixxat/canlib_vcinpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ def _send_periodic_internal(
msgs: Union[Sequence[Message], Message],
period: float,
duration: Optional[float] = None,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> CyclicSendTaskABC:
"""Send a message using built-in cyclic transmit list functionality."""
Expand Down Expand Up @@ -821,6 +822,7 @@ def _send_periodic_internal(
msgs=msgs,
period=period,
duration=duration,
autostart=autostart,
modifier_callback=modifier_callback,
)

Expand Down
2 changes: 2 additions & 0 deletions can/interfaces/ixxat/canlib_vcinpl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ def _send_periodic_internal(
msgs: Union[Sequence[Message], Message],
period: float,
duration: Optional[float] = None,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> CyclicSendTaskABC:
"""Send a message using built-in cyclic transmit list functionality."""
Expand Down Expand Up @@ -967,6 +968,7 @@ def _send_periodic_internal(
msgs=msgs,
period=period,
duration=duration,
autostart=autostart,
modifier_callback=modifier_callback,
)

Expand Down
7 changes: 6 additions & 1 deletion can/interfaces/socketcan/socketcan.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ def _send_periodic_internal(
msgs: Union[Sequence[Message], Message],
period: float,
duration: Optional[float] = None,
autostart: bool = True,
modifier_callback: Optional[Callable[[Message], None]] = None,
) -> can.broadcastmanager.CyclicSendTaskABC:
"""Start sending messages at a given period on this bus.
Expand All @@ -823,13 +824,16 @@ def _send_periodic_internal(
:class:`CyclicSendTask` within BCM provides flexibility to schedule
CAN messages sending with the same CAN ID, but different CAN data.
:param messages:
:param msgs:
The message(s) to be sent periodically.
:param period:
The rate in seconds at which to send the messages.
:param duration:
Approximate duration in seconds to continue sending messages. If
no duration is provided, the task will continue indefinitely.
:param autostart:
If True (the default) the sending task will immediately start after creation.
Otherwise, the task has to be started by calling start() on it.
:raises ValueError:
If task identifier passed to :class:`CyclicSendTask` can't be used
Expand Down Expand Up @@ -868,6 +872,7 @@ def _send_periodic_internal(
msgs=msgs,
period=period,
duration=duration,
autostart=autostart,
modifier_callback=modifier_callback,
)

Expand Down

0 comments on commit 56262ea

Please sign in to comment.