1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-11-26 18:08:36 -05:00

Added APRSDThread pause/unpause capability

This patch adds the logical ability to pause and unpause a thread.
This enables the base class decide to call the main loop() method or
not.  If the thread is paused, it will simply call sleep for a second
and then continue.  Only when the thread is unpaused will the main
loop() get called again.
This commit is contained in:
Hemna 2024-11-22 10:45:22 -05:00
parent e99c906fed
commit 95094b874c

View File

@ -2,6 +2,7 @@ import abc
import datetime
import logging
import threading
import time
from typing import List
import wrapt
@ -14,6 +15,8 @@ class APRSDThread(threading.Thread, metaclass=abc.ABCMeta):
"""Base class for all threads in APRSD."""
loop_count = 1
_pause = False
thread_stop = False
def __init__(self, name):
super().__init__(name=name)
@ -26,6 +29,16 @@ class APRSDThread(threading.Thread, metaclass=abc.ABCMeta):
if self.thread_stop:
return True
def pause(self):
"""Logically pause the processing of the main loop."""
LOG.debug(f"Pausing thread '{self.name}' loop_count {self.loop_count}")
self._pause = True
def unpause(self):
"""Logically resume processing of the main loop."""
LOG.debug(f"Resuming thread '{self.name}' loop_count {self.loop_count}")
self._pause = False
def stop(self):
self.thread_stop = True
@ -47,6 +60,9 @@ class APRSDThread(threading.Thread, metaclass=abc.ABCMeta):
def run(self):
LOG.debug("Starting")
while not self._should_quit():
if self._pause:
time.sleep(1)
else:
self.loop_count += 1
can_loop = self.loop()
self._last_loop = datetime.datetime.now()