Use Dispatch thread for processing#65
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a dedicated ECU-owned dispatch thread and FIFO queue to decouple python-can’s Notifier reception path from J1939 frame processing (including subscriber callbacks), aiming to reduce latency and avoid frame bursts caused by slow callbacks while preserving frame ordering and draining in-flight frames on shutdown.
Changes:
- Added an ECU dispatch thread (
_dispatch_thread) and dispatch queue (_dispatch_queue) to serialize processing of received CAN frames off the Notifier thread. - Updated
ElectronicControlUnit.notify()to enqueue frames instead of directly invokingj1939_dll.notify(). - Expanded
test/test_threading.pywith new tests covering dispatch thread existence/naming, notify non-blocking behavior, ordering, and shutdown drain behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
j1939/electronic_control_unit.py |
Adds a dispatch thread + queue and routes notify() through it; updates shutdown to join the new thread. |
test/test_threading.py |
Adds threading tests for dispatch queue behavior and refactors formatting for readability. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
khauersp
marked this pull request as ready for review
July 21, 2026 19:22
snideto
previously approved these changes
Jul 21, 2026
drewr95
approved these changes
Jul 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a new dispatch thread to the
ElectronicControlUnitclass inj1939/electronic_control_unit.pyto improve CAN frame handling and overall concurrency. The main goal is to decouple frame reception from frame processing, reducing latency and preventing frame bursts caused by subscriber callback delays. The most important changes are as follows:This is a move towards trying to help with some thread issues we are seeing and not going full force into using async yet
Concurrency and Frame Dispatching Improvements:
_dispatch_thread) and a FIFO queue (_dispatch_queue) to serially process incoming CAN frames, ensuring the Notifier thread returns quickly and reducing GIL contention.notify()method to enqueue frames onto the dispatch queue instead of callingj1939_dll.notify()directly, preventing the Notifier thread from being blocked by slow subscriber callbacks. [1] [2]_dispatch_job_thread()method, which drains the queue and calls the DLL, preserving frame order and ensuring all in-flight frames are processed even during shutdown.Lifecycle Management:
stop()method to join the new dispatch thread, ensuring clean shutdown and that all queued frames are processed before exit.