strix

A simple web UI for motion
git clone https://www.brianlane.com/git/strix
Log | Files | Refs | LICENSE

commit 68ac35d1a5120476c0caa1bfd34c923714ca569f
parent db2ef4c0c7a3123806c9e0cb80e518c41c24961c
Author: Brian C. Lane <bcl@brianlane.com>
Date:   Wed, 24 Feb 2021 07:15:39 -0800

Add --max-cores option

Default to number of CPUs reported by multiprocessing / 2, but allow
that to be overridden by passing --max-cores=X

Diffstat:
Msrc/strix/__init__.py | 4++--
Msrc/strix/cmdline.py | 8+++++++-
Msrc/strix/queue.py | 8+++++---
3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/strix/__init__.py b/src/strix/__init__.py @@ -87,7 +87,7 @@ def check_motion_config(config_path: str) -> Tuple[str, List[str]]: def run() -> bool: - parser = cmdline.parser() + parser = cmdline.parser(queue.max_cores()) opts = parser.parse_args() try: @@ -118,7 +118,7 @@ def run() -> bool: queue_quit = mp.Event() queue_thread = mp.Process(name="queue-thread", target=queue.monitor_queue, - args=(logging_queue, base_dir, queue_quit)) + args=(logging_queue, base_dir, queue_quit, opts.max_cores)) queue_thread.start() running_threads += [(queue_thread, queue_quit)] diff --git a/src/strix/cmdline.py b/src/strix/cmdline.py @@ -18,7 +18,7 @@ import argparse version = "DEVEL" -def parser() -> argparse.ArgumentParser: +def parser(max_cores) -> argparse.ArgumentParser: """ Return the ArgumentParser""" parser = argparse.ArgumentParser(description="Motion Camera Web Interface") @@ -48,6 +48,12 @@ def parser() -> argparse.ArgumentParser: optional.add_argument("--debug", help="Output debug information", action="store_true", default=False) + optional.add_argument("--max-cores", + help="Maximum cores to use for ffmpeg", + metavar="MAXCORES", + type=int, + default=max_cores) + # add the show version option parser.add_argument("-V", help="show program's version number and exit", diff --git a/src/strix/queue.py b/src/strix/queue.py @@ -30,6 +30,9 @@ from . import logger THUMBNAIL_SIZE = (640, 480) +def max_cores() -> int: + return max(1, mp.cpu_count() // 2) + ## Handle watching the queue and dispatching movie creation and directory moving def process_event(log: structlog.BoundLogger, base_dir: str, event: str) -> None: @@ -95,7 +98,7 @@ def process_event(log: structlog.BoundLogger, base_dir: str, event: str) -> None except Exception as e: log.error("Moving to destination failed", event_path=event_path, exception=str(e)) -def monitor_queue(logging_queue: mp.Queue, base_dir: str, quit: mp.Event) -> None: +def monitor_queue(logging_queue: mp.Queue, base_dir: str, quit: mp.Event, max_threads: int) -> None: threads = [] # type: List[mp.Process] log = logger.log(logging_queue) @@ -108,10 +111,9 @@ def monitor_queue(logging_queue: mp.Queue, base_dir: str, quit: mp.Event) -> Non if not t.is_alive(): threads.remove(t) - log.debug("queue check", queue_path=queue_path) for event_file in glob(os.path.join(queue_path, "*")): # Limit the number of processes to 1/2 the number of cpus (or 1) - if len(threads) >= max(1, mp.cpu_count() // 2): + if len(threads) >= max_threads: break os.unlink(event_file)