From 86683ff5c3cc7df74d89fead8b2b2ab0e6b4b972 Mon Sep 17 00:00:00 2001 From: nharris Date: Mon, 27 Feb 2023 22:20:08 -0700 Subject: [PATCH] - Lock multithreading for performance --- dizqueTV/helpers.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dizqueTV/helpers.py b/dizqueTV/helpers.py index 3a911fa..f576d15 100644 --- a/dizqueTV/helpers.py +++ b/dizqueTV/helpers.py @@ -21,7 +21,7 @@ # Internal Helpers def _multithread( - func, elements: List, element_param_name: str, thread_count: int = 20, **kwargs + func, elements: List, element_param_name: str, thread_count: int = None, **kwargs ) -> List: """ Multithread a function for elements in a list @@ -39,6 +39,11 @@ def _multithread( :return: List of results from the function :rtype: list """ + # Thread count is the smallest of the following: + # - Number of elements in the list + # - Number of CPU cores * 8 (arbitrary) + # Or override with thread_count + thread_count = thread_count or min(len(element_param_name), (os.cpu_count() * 8)) thread_list = [] pool = ThreadPoolExecutor(thread_count)