Skip to content

Instantly share code, notes, and snippets.

@yadudoc
Last active November 11, 2019 20:20
Show Gist options
  • Save yadudoc/b19c8b7821068ab510fd9b317d748774 to your computer and use it in GitHub Desktop.
Save yadudoc/b19c8b7821068ab510fd9b317d748774 to your computer and use it in GitHub Desktop.
# We'd ideally want to import our modules from a separate module :
# import methods
class MpiMethodServer:
def __init__(self, input_queue, output_queue, methods_list=None, load_default=True):
""" If load_default = True, we load the default methods list from a separate methods file
else the user must pass a list of methods via methods_list kwarg.
"""
self.input_queue = input_queue
self.output_queue = output_queue
self.task_list = []
self.methods_table = {} # Dict maps {func_name : func}
if load_default is True:
for method in methods.methods_list:
self.add_method(method)
else:
for method in methods_list:
self.add_method(method)
def add_method(self.method):
# Python function's name can be accessed as a string via __name__
self.methods_table[method.__name__] = method
def launch_method(self, method_name, *args, **kwargs):
if method_name in self.methods_table:
x = self.methods_table[method_name](*args, **kwargs)
return x
else:
print(f"Requested method : {method_name} is not loaded")
raise KeyError(f"Missing {method_name} in methods_table")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment