1
0
mirror of https://github.com/slgobinath/SafeEyes.git synced 2024-12-29 00:20:28 +01:00

Initialize thread pool only if there are plugins

This commit is contained in:
Gobinath 2017-04-21 10:33:24 -04:00
parent 5e9f68ff44
commit a1b3f6e094

View File

@ -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):
"""