Refactor threads to use daemon threads and Event-based timing#219
Merged
Refactor threads to use daemon threads and Event-based timing#219
Conversation
…hread - Add daemon=True class attribute (subclasses override to False) - Add period=1 class attribute for wait interval - Replace thread_stop boolean with _shutdown_event (threading.Event) - Add wait() method for interruptible sleeps - Update tests for new Event-based API BREAKING: thread_stop boolean replaced with _shutdown_event. Code checking thread.thread_stop directly must use thread._shutdown_event.is_set()
Allows graceful shutdown by waiting for non-daemon threads to complete while allowing daemon threads to be terminated immediately.
- APRSDRXThread: Replace time.sleep with self.wait for interruptible waits - APRSDRXThread.stop(): Use _shutdown_event.set() instead of thread_stop - APRSDRXThread: Error recovery waits check for shutdown signal - APRSDFilterThread: Use queue timeout with self.period for interruptible wait - Remove unused time import - Update tests to use new Event-based API
- PacketSendSchedulerThread: Add daemon=False, replace time.sleep with self.wait - AckSendSchedulerThread: Add daemon=False, replace time.sleep with self.wait - SendPacketThread: Replace time.sleep with self.wait, remove manual loop_count - SendAckThread: Replace time.sleep with self.wait, remove manual loop_count - BeaconSendThread: Set self.period=CONF.beacon_interval, remove counter-based conditional, replace time.sleep with self.wait, remove _loop_cnt tracking - Update tests to use new Event-based API
- Set self.period=CONF.aprs_registry.frequency_seconds in __init__ - Remove counter-based conditional (loop every N seconds pattern) - Replace time.sleep(1) with self.wait() - Remove _loop_cnt tracking (use inherited loop_count from base) - Remove unused time import
- Replace time.sleep(1.5) with thread_list.join_non_daemon(timeout=5.0) - Remove unused import time since time.sleep is no longer used - Remove outdated commented-out code - Improve log message (removed '10 seconds' reference)
- Add -> bool return type annotation to abstract loop() method - Replace 'from typing import List' with built-in list[] (Python 3.9+)
08636f5 to
8d8648e
Compare
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.
Summary
This PR refactors all APRSD thread classes to:
threading.Event()for interruptible waitsChanges
Base Class (
aprsd/threads/aprsd.py)daemon = Trueclass attribute (subclasses override toFalse)period = 1class attribute for wait intervalthread_stopboolean with_shutdown_event(threading.Event)wait()method for interruptible sleepsjoin_non_daemon()toAPRSDThreadListfor graceful shutdownNon-Daemon Threads (3)
These threads are marked
daemon = Falsefor graceful shutdown:PacketSendSchedulerThread- manages packet send queueAckSendSchedulerThread- manages ACK send queueAPRSDStatsStoreThread- writes stats to diskThread Migrations
All threads migrated from counter-based timing to period-based:
KeepAliveThread- period=60APRSDStatsStoreThread- period=10APRSDPushStatsThread- period from configStatsLogThread- period=10APRSDRXThread- interruptible error recoveryAPRSDFilterThread- queue timeout as waitBeaconSendThread- period from configAPRSRegistryThread- period from configSignal Handler
time.sleep(1.5)withjoin_non_daemon(timeout=5.0)Benefits
time.sleep()to finishloop_count % 60 == 0)Testing
Breaking Changes
thread_stopboolean replaced with_shutdown_eventthread.thread_stopdirectly must usethread._shutdown_event.is_set()