Skip to content

Commit 44ccc7e

Browse files
committed
Backends now use a separate method to implement periodic sending.
1 parent cdecd3a commit 44ccc7e

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

can/bus.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ def send_periodic(self, msg, period, duration=None, store_task=True):
199199
api with ``store_task==True`` may not be appropriate as the stopped tasks are
200200
still taking up memory as they are associated with the Bus instance.
201201
"""
202-
if not hasattr(self, "_lock_send_periodic"):
203-
# Create a send lock for this bus
204-
self._lock_send_periodic = threading.Lock()
205-
task = ThreadBasedCyclicSendTask(self, self._lock_send_periodic, msg, period, duration)
202+
task = self._send_periodic_internal(msg, period, duration)
206203
# we wrap the task's stop method to also remove it from the Bus's list of tasks
207204
original_stop_method = task.stop
208205

@@ -214,8 +211,33 @@ def wrapped_stop_method(remove_task=True):
214211
pass
215212
original_stop_method()
216213
task.stop = wrapped_stop_method
214+
217215
if store_task:
218216
self._periodic_tasks.append(task)
217+
218+
return task
219+
220+
def _send_periodic_internal(self, msg, period, duration=None):
221+
"""Default implementation of periodic message sending using threading.
222+
223+
Override this method to enable a more efficient backend specific approach.
224+
225+
:param can.Message msg:
226+
Message to transmit
227+
:param float period:
228+
Period in seconds between each message
229+
:param float duration:
230+
The duration to keep sending this message at given rate. If
231+
no duration is provided, the task will continue indefinitely.
232+
:return:
233+
A started task instance. Note the task can be stopped (and depending on
234+
the backend modified) by calling the :meth:`stop` method.
235+
:rtype: can.broadcastmanager.CyclicSendTaskABC
236+
"""
237+
if not hasattr(self, "_lock_send_periodic"):
238+
# Create a send lock for this bus
239+
self._lock_send_periodic = threading.Lock()
240+
task = ThreadBasedCyclicSendTask(self, self._lock_send_periodic, msg, period, duration)
219241
return task
220242

221243
def stop_all_periodic_tasks(self, remove_tasks=True):

can/interfaces/ixxat/canlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def send(self, msg, timeout=None):
500500
else:
501501
_canlib.canChannelPostMessage(self._channel_handle, message)
502502

503-
def send_periodic(self, msg, period, duration=None):
503+
def _send_periodic_internal(self, msg, period, duration=None):
504504
"""Send a message using built-in cyclic transmit list functionality."""
505505
if self._scheduler is None:
506506
self._scheduler = HANDLE()

can/interfaces/socketcan/socketcan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ def _send_once(self, data, channel=None):
571571
raise can.CanError("Failed to transmit: %s" % exc)
572572
return sent
573573

574-
def send_periodic(self, msg, period, duration=None):
574+
def _send_periodic_internal(self, msg, period, duration=None):
575575
"""Start sending a message at a given period on this bus.
576576
577577
The kernel's broadcast manager will be used.

doc/development.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ Creating a new interface/backend
3232

3333
These steps are a guideline on how to add a new backend to python-can.
3434

35-
- Create a module (either a ``*.py`` or an entire subdirctory depending
35+
- Create a module (either a ``*.py`` or an entire subdirectory depending
3636
on the complexity) inside ``can.interfaces``
3737
- Implement the central part of the backend: the bus class that extends
3838
:class:`can.BusABC`. See below for more info on this one!
3939
- Register your backend bus class in ``can.interface.BACKENDS`` and
40-
``can.interfaces.VALID_INTERFACES``.
41-
- Add docs where appropiate, like in ``doc/interfaces.rst`` and add
42-
an entry in ``doc/interface/*``.
43-
Update ``doc/scripts.rst`` accordingly.
44-
- Add tests in ``test/*`` where appropiate.
40+
``can.interfaces.VALID_INTERFACES`` in ``can.interfaces.__init__.py``.
41+
- Add docs where appropriate. At a minimum add to ``doc/interfaces.rst`` and add
42+
a new interface specific document in ``doc/interface/*``.
43+
- Update ``doc/scripts.rst`` accordingly.
44+
- Add tests in ``test/*`` where appropriate.
4545

4646

4747
About the ``BusABC`` class
@@ -59,15 +59,15 @@ They *might* implement the following:
5959
messages yet to be sent
6060
* :meth:`~can.BusABC.shutdown` to override how the bus should
6161
shut down
62-
* :meth:`~can.BusABC.send_periodic` to override the software based
63-
periodic sending and push it down to the kernel or hardware
62+
* :meth:`~can.BusABC._send_periodic_internal` to override the software based
63+
periodic sending and push it down to the kernel or hardware.
6464
* :meth:`~can.BusABC._apply_filters` to apply efficient filters
65-
to lower level systems like the OS kernel or hardware
65+
to lower level systems like the OS kernel or hardware.
6666
* :meth:`~can.BusABC._detect_available_configs` to allow the interface
6767
to report which configurations are currently available for new
68-
connections
68+
connections.
6969
* :meth:`~can.BusABC.state` property to allow reading and/or changing
70-
the bus state
70+
the bus state.
7171

7272
.. note::
7373

0 commit comments

Comments
 (0)