mirror of
https://github.com/craigerl/aprsd.git
synced 2025-02-03 09:44:15 -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:
parent
e99c906fed
commit
95094b874c
@ -2,6 +2,7 @@ import abc
|
|||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import wrapt
|
import wrapt
|
||||||
@ -14,6 +15,8 @@ class APRSDThread(threading.Thread, metaclass=abc.ABCMeta):
|
|||||||
"""Base class for all threads in APRSD."""
|
"""Base class for all threads in APRSD."""
|
||||||
|
|
||||||
loop_count = 1
|
loop_count = 1
|
||||||
|
_pause = False
|
||||||
|
thread_stop = False
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super().__init__(name=name)
|
super().__init__(name=name)
|
||||||
@ -26,6 +29,16 @@ class APRSDThread(threading.Thread, metaclass=abc.ABCMeta):
|
|||||||
if self.thread_stop:
|
if self.thread_stop:
|
||||||
return True
|
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):
|
def stop(self):
|
||||||
self.thread_stop = True
|
self.thread_stop = True
|
||||||
|
|
||||||
@ -47,6 +60,9 @@ class APRSDThread(threading.Thread, metaclass=abc.ABCMeta):
|
|||||||
def run(self):
|
def run(self):
|
||||||
LOG.debug("Starting")
|
LOG.debug("Starting")
|
||||||
while not self._should_quit():
|
while not self._should_quit():
|
||||||
|
if self._pause:
|
||||||
|
time.sleep(1)
|
||||||
|
else:
|
||||||
self.loop_count += 1
|
self.loop_count += 1
|
||||||
can_loop = self.loop()
|
can_loop = self.loop()
|
||||||
self._last_loop = datetime.datetime.now()
|
self._last_loop = datetime.datetime.now()
|
||||||
|
Loading…
Reference in New Issue
Block a user