From 074e03af0de205d2cc734ab818f5fffcf5d332a1 Mon Sep 17 00:00:00 2001 From: pierreluctg Date: Wed, 27 Sep 2023 16:02:44 -0400 Subject: [PATCH 1/4] We do not need to account for drift when USE_WINDOWS_EVENTS --- can/broadcastmanager.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/can/broadcastmanager.py b/can/broadcastmanager.py index a017b7d52..d2e9f9669 100644 --- a/can/broadcastmanager.py +++ b/can/broadcastmanager.py @@ -35,7 +35,6 @@ log = logging.getLogger("can.bcm") NANOSECONDS_IN_SECOND: Final[int] = 1_000_000_000 -NANOSECONDS_IN_MILLISECOND: Final[int] = 1_000_000 class CyclicTask(abc.ABC): @@ -316,19 +315,19 @@ def _run(self) -> None: self.stop() break - msg_due_time_ns += self.period_ns + if not USE_WINDOWS_EVENTS: + msg_due_time_ns += self.period_ns if self.end_time is not None and time.perf_counter() >= self.end_time: break msg_index = (msg_index + 1) % len(self.messages) - # Compensate for the time it takes to send the message - delay_ns = msg_due_time_ns - time.perf_counter_ns() - - if delay_ns > 0: - if USE_WINDOWS_EVENTS: - win32event.WaitForSingleObject( - self.event.handle, - int(round(delay_ns / NANOSECONDS_IN_MILLISECOND)), - ) - else: + if USE_WINDOWS_EVENTS: + win32event.WaitForSingleObject( + self.event.handle, + -1, + ) + else: + # Compensate for the time it takes to send the message + delay_ns = msg_due_time_ns - time.perf_counter_ns() + if delay_ns > 0: time.sleep(delay_ns / NANOSECONDS_IN_SECOND) From c455d71b157861c15815310b3688e0e23232a65c Mon Sep 17 00:00:00 2001 From: pierreluctg Date: Wed, 27 Sep 2023 16:10:56 -0400 Subject: [PATCH 2/4] Update broadcastmanager.py --- can/broadcastmanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can/broadcastmanager.py b/can/broadcastmanager.py index d2e9f9669..398114a59 100644 --- a/can/broadcastmanager.py +++ b/can/broadcastmanager.py @@ -324,7 +324,7 @@ def _run(self) -> None: if USE_WINDOWS_EVENTS: win32event.WaitForSingleObject( self.event.handle, - -1, + win32event.INFINITE, ) else: # Compensate for the time it takes to send the message From ad7247cf123a77e309352a95c345373496ac59c0 Mon Sep 17 00:00:00 2001 From: pierreluctg Date: Wed, 27 Sep 2023 16:12:55 -0400 Subject: [PATCH 3/4] Update broadcastmanager.py --- can/broadcastmanager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/can/broadcastmanager.py b/can/broadcastmanager.py index 398114a59..ab2a5a032 100644 --- a/can/broadcastmanager.py +++ b/can/broadcastmanager.py @@ -289,11 +289,12 @@ def start(self) -> None: def _run(self) -> None: msg_index = 0 - msg_due_time_ns = time.perf_counter_ns() if USE_WINDOWS_EVENTS: # Make sure the timer is non-signaled before entering the loop win32event.WaitForSingleObject(self.event.handle, 0) + else: + msg_due_time_ns = time.perf_counter_ns() while not self.stopped: # Prevent calling bus.send from multiple threads From 640589fc192dddd47b005e279173a61b2142aaf9 Mon Sep 17 00:00:00 2001 From: pierreluctg Date: Mon, 2 Oct 2023 09:15:45 -0400 Subject: [PATCH 4/4] Update broadcastmanager.py --- can/broadcastmanager.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/can/broadcastmanager.py b/can/broadcastmanager.py index ab2a5a032..398114a59 100644 --- a/can/broadcastmanager.py +++ b/can/broadcastmanager.py @@ -289,12 +289,11 @@ def start(self) -> None: def _run(self) -> None: msg_index = 0 + msg_due_time_ns = time.perf_counter_ns() if USE_WINDOWS_EVENTS: # Make sure the timer is non-signaled before entering the loop win32event.WaitForSingleObject(self.event.handle, 0) - else: - msg_due_time_ns = time.perf_counter_ns() while not self.stopped: # Prevent calling bus.send from multiple threads