From a1b3f6e0943bb1804b88b1d2fe642cb985a2dd01 Mon Sep 17 00:00:00 2001 From: Gobinath Date: Fri, 21 Apr 2017 10:33:24 -0400 Subject: [PATCH] Initialize thread pool only if there are plugins --- safeeyes/Plugins.py | 104 ++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/safeeyes/Plugins.py b/safeeyes/Plugins.py index 2546d7c..93e5ab9 100644 --- a/safeeyes/Plugins.py +++ b/safeeyes/Plugins.py @@ -35,7 +35,6 @@ class Plugins: """ logging.info('Load all the plugins') self.__plugins = [] - self.__thread_pool = ThreadPool(4) for plugin in config['plugins']: if plugin['location'].lower() in ['left', 'right']: @@ -50,28 +49,34 @@ class Plugins: else: logging.warning('Ignoring the plugin ' + str(plugin['name']) + ' due to invalid location value: ' + plugin['location']) + print(min([4, len(self.__plugins)])) + if self.__plugins: + self.__thread_pool = ThreadPool(min([4, len(self.__plugins)])) + def start(self, context): """ Call the start function of all the plugins in separate thread. """ - context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes - for plugin in self.__plugins: - try: - self.__thread_pool.apply_async(plugin['module'].start, (context,)) - except Exception as e: - pass + if self.__plugins: + context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes + for plugin in self.__plugins: + try: + self.__thread_pool.apply_async(plugin['module'].start, (context,)) + except Exception as e: + pass def pre_notification(self, context): """ Call the pre_notification function of all the plugins in separate thread. """ - context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes - for plugin in self.__plugins: - try: - self.__thread_pool.apply_async(plugin['module'].pre_notification, (context,)) - except Exception as e: - pass + if self.__plugins: + context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes + for plugin in self.__plugins: + try: + self.__thread_pool.apply_async(plugin['module'].pre_notification, (context,)) + except Exception as e: + pass def pre_break(self, context): @@ -81,57 +86,60 @@ class Plugins: Returns: {'left': 'Markup of plugins to be aligned on left', 'right': 'Markup of plugins to be aligned on right' } """ - context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes - output = {'left': ' \n', 'right': ' \n'} - multiple_results = [self.__thread_pool.apply_async(plugin['module'].pre_break, (context,)) for plugin in self.__plugins] - for i in range(len(multiple_results)): - try: - result = multiple_results[i].get(timeout=1) - if result: - # Limit the line length to 50 characters - large_lines = list(filter(lambda x: len(x) > 50, Utility.html_to_text(result).splitlines())) - if large_lines: - logging.warning('Ignoring lengthy result from the plugin ' + self.__plugins[i]['name']) - continue - output[self.__plugins[i]['location']] += (result + '\n\n') - except Exception: - # Something went wrong in the plugin - logging.warning('Error when executing the plugin ' + self.__plugins[i]['name']) + if self.__plugins: + context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes + output = {'left': ' \n', 'right': ' \n'} + multiple_results = [self.__thread_pool.apply_async(plugin['module'].pre_break, (context,)) for plugin in self.__plugins] + for i in range(len(multiple_results)): + try: + result = multiple_results[i].get(timeout=1) + if result: + # Limit the line length to 50 characters + large_lines = list(filter(lambda x: len(x) > 50, Utility.html_to_text(result).splitlines())) + if large_lines: + logging.warning('Ignoring lengthy result from the plugin ' + self.__plugins[i]['name']) + continue + output[self.__plugins[i]['location']] += (result + '\n\n') + except Exception: + # Something went wrong in the plugin + logging.warning('Error when executing the plugin ' + self.__plugins[i]['name']) - return output + return output def post_break(self, context): """ Call the post_break function of all the plugins in separate thread. """ - context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes - for plugin in self.__plugins: - try: - self.__thread_pool.apply_async(plugin['module'].post_break, (context,)) - except Exception as e: - pass + if self.__plugins: + context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes + for plugin in self.__plugins: + try: + self.__thread_pool.apply_async(plugin['module'].post_break, (context,)) + except Exception as e: + pass def exit(self, context): """ Call the exit function of all the plugins in separate thread. """ - context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes + if self.__plugins: + context = copy.deepcopy(context) # If plugins change the context, it should not affect Safe Eyes + + # Give maximum 1 sec for all plugins before terminating the thread pool + multiple_results = [self.__thread_pool.apply_async(plugin['module'].exit, (context,)) for plugin in self.__plugins] + for i in range(len(multiple_results)): + try: + multiple_results[i].get(timeout=1) + except Exception: + # Something went wrong in the plugin + pass - # Give maximum 1 sec for all plugins before terminating the thread pool - multiple_results = [self.__thread_pool.apply_async(plugin['module'].exit, (context,)) for plugin in self.__plugins] - for i in range(len(multiple_results)): try: - multiple_results[i].get(timeout=1) - except Exception: - # Something went wrong in the plugin + self.__thread_pool.terminate() # Shutdown the pool + except Exception as e: pass - try: - self.__thread_pool.terminate() # Shutdown the pool - except Exception as e: - pass - def __has_method(self, module, method_name, no_of_args = 1): """