Skip to content

Use Dispatch thread for processing#65

Merged
khauersp merged 6 commits into
masterfrom
feature/use-notifier-queue
Jul 22, 2026
Merged

Use Dispatch thread for processing#65
khauersp merged 6 commits into
masterfrom
feature/use-notifier-queue

Conversation

@khauersp

@khauersp khauersp commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

This pull request introduces a new dispatch thread to the ElectronicControlUnit class in j1939/electronic_control_unit.py to 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:

  • Added a dedicated dispatch thread (_dispatch_thread) and a FIFO queue (_dispatch_queue) to serially process incoming CAN frames, ensuring the Notifier thread returns quickly and reducing GIL contention.
  • Modified the notify() method to enqueue frames onto the dispatch queue instead of calling j1939_dll.notify() directly, preventing the Notifier thread from being blocked by slow subscriber callbacks. [1] [2]
  • Implemented the _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:

  • Updated the stop() method to join the new dispatch thread, ensuring clean shutdown and that all queued frames are processed before exit.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 invoking j1939_dll.notify().
  • Expanded test/test_threading.py with 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.

Comment thread j1939/electronic_control_unit.py Outdated
Comment thread j1939/electronic_control_unit.py Outdated
Comment thread j1939/electronic_control_unit.py
Comment thread j1939/electronic_control_unit.py Outdated
Comment thread test/test_threading.py Outdated
@khauersp
khauersp marked this pull request as ready for review July 21, 2026 19:22
@khauersp khauersp changed the title Use notifier queue Use Dispatch thread for processing Jul 21, 2026
snideto
snideto previously approved these changes Jul 21, 2026
@khauersp
khauersp merged commit 70768f8 into master Jul 22, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants