Skip to content

Commit 3eb1c7b

Browse files
Make thread based periodic tasks restartable
1 parent 8f8ee43 commit 3eb1c7b

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

can/broadcastmanager.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,26 @@ def _transmit(self, task):
122122

123123

124124
class ThreadBasedCyclicSendTask(ModifiableCyclicTaskABC,
125-
LimitedDurationCyclicSendTaskABC):
125+
LimitedDurationCyclicSendTaskABC,
126+
RestartableCyclicTaskABC):
126127
"""Fallback cyclic send task using thread."""
127128

128-
def __init__(self, message, period, duration=None):
129+
def __init__(self, bus, message, period, duration=None):
129130
super(ThreadBasedCyclicSendTask, self).__init__(message, period, duration)
131+
if not hasattr(bus, "cyclic_manager"):
132+
bus.cyclic_manager = ThreadBasedCyclicSendManager(bus.send)
133+
self.bus = bus
130134
self.stopped = False
131135
self.end_time = time.time() + duration if duration else None
136+
self.start()
132137

133138
def stop(self):
134139
self.stopped = True
135140

141+
def start(self):
142+
self.stopped = False
143+
self.bus.cyclic_manager.add_task(self)
144+
136145

137146
def send_periodic(bus, message, period):
138147
"""

can/bus.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,16 @@ def send_periodic(self, msg, period, duration=None):
6767
"""Start sending a message at a given period on this bus.
6868
6969
:param can.Message msg:
70+
Message to transmit
7071
:param float period:
72+
Period in seconds between each message
7173
:param float duration:
7274
The duration to keep sending this message at given rate. If
7375
no duration is provided, the task will continue indefinitely.
7476
:return: A started task instance
7577
:rtype: can.CyclicSendTaskABC
7678
"""
77-
if not hasattr(self, "cyclic_manager"):
78-
self.cyclic_manager = ThreadBasedCyclicSendManager(self.send)
79-
task = ThreadBasedCyclicSendTask(msg, period, duration)
80-
self.cyclic_manager.add_task(task)
81-
return task
79+
return ThreadBasedCyclicSendTask(self, msg, period, duration)
8280

8381
def __iter__(self):
8482
"""Allow iteration on messages as they are received.

can/interfaces/interface.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,12 @@ def __new__(cls, other, bus, *args, **kwargs):
104104
elif can.rc['interface'] == 'socketcan_native':
105105
from can.interfaces.socketcan.socketcan_native import CyclicSendTask as _nativeCyclicSendTask
106106
cls = _nativeCyclicSendTask
107-
# CyclicSendTask has not been fully implemented on remote interface yet.
108-
# Waiting for issue #80 which will change the API to make it easier for
109-
# interfaces other than socketcan to implement it
110-
#elif can.rc['interface'] == 'remote':
111-
# from can.interfaces.remote import CyclicSendTask as _remoteCyclicSendTask
112-
# cls = _remoteCyclicSendTask
107+
elif can.rc['interface'] == 'remote':
108+
from can.interfaces.remote import CyclicSendTask as _remoteCyclicSendTask
109+
cls = _remoteCyclicSendTask
113110
else:
114-
can.log.info("Current CAN interface doesn't support CyclicSendTask")
111+
from can.broadcastmanager import ThreadBasedCyclicSendTask
112+
cls = ThreadBasedCyclicSendTask
115113

116114
return cls(bus, *args, **kwargs)
117115

0 commit comments

Comments
 (0)