|
@@ -8,6 +8,10 @@ logging.basicConfig()
|
|
|
logger = logging.getLogger("kalliope")
|
|
|
|
|
|
|
|
|
+class Serialize(Exception): pass
|
|
|
+class Synapse_LIFO(Exception): pass
|
|
|
+
|
|
|
+
|
|
|
class LIFOBuffer(object):
|
|
|
"""
|
|
|
This class is a LIFO list of synapse to process where the last synapse list to enter will be the first synapse
|
|
@@ -21,7 +25,6 @@ class LIFOBuffer(object):
|
|
|
|
|
|
api_response = APIResponse()
|
|
|
lifo_list = list()
|
|
|
- synapse_list_added_to_lifo = False
|
|
|
logger.debug("[LIFOBuffer] LIFO buffer created")
|
|
|
|
|
|
@classmethod
|
|
@@ -49,83 +52,75 @@ class LIFOBuffer(object):
|
|
|
:param is_api_call: Boolean passed to all neuron in order to let them know if the current call comes from API
|
|
|
:return: serialized APIResponse object
|
|
|
"""
|
|
|
+ try:
|
|
|
+
|
|
|
+ while cls.lifo_list:
|
|
|
+ try:
|
|
|
+ logger.debug("[LIFOBuffer] number of synapse list to process: %s" % len(cls.lifo_list))
|
|
|
+
|
|
|
+
|
|
|
+ last_synapse_fifo_list = cls.lifo_list[-1]
|
|
|
+
|
|
|
+ while last_synapse_fifo_list:
|
|
|
+
|
|
|
+ matched_synapse = last_synapse_fifo_list[0]
|
|
|
+
|
|
|
+
|
|
|
+ if matched_synapse not in cls.api_response.list_processed_matched_synapse:
|
|
|
+ cls.api_response.list_processed_matched_synapse.append(matched_synapse)
|
|
|
+
|
|
|
+ while matched_synapse.neuron_fifo_list:
|
|
|
+
|
|
|
+ logger.debug("[LIFOBuffer] number of neuron to process: %s" % len(matched_synapse.neuron_fifo_list))
|
|
|
+
|
|
|
+ neuron = matched_synapse.neuron_fifo_list[0]
|
|
|
+
|
|
|
+ if answer is not None:
|
|
|
+ neuron.parameters["answer"] = answer
|
|
|
+
|
|
|
+ answer = None
|
|
|
+
|
|
|
+ neuron.parameters["is_api_call"] = is_api_call
|
|
|
+
|
|
|
+ instantiated_neuron = NeuronLauncher.start_neuron(neuron=neuron,
|
|
|
+ parameters_dict=matched_synapse.parameters)
|
|
|
+
|
|
|
+
|
|
|
+ cls.api_response.status = "complete"
|
|
|
+ if instantiated_neuron.is_waiting_for_answer:
|
|
|
+ logger.debug("[LIFOBuffer] Wait for answer mode")
|
|
|
+ cls.api_response.status = "waiting_for_answer"
|
|
|
+ raise Serialize
|
|
|
+ else:
|
|
|
+ logger.debug("[LIFOBuffer] complete mode")
|
|
|
+
|
|
|
+
|
|
|
+ matched_synapse.neuron_module_list.append(instantiated_neuron)
|
|
|
+
|
|
|
+ matched_synapse.neuron_fifo_list.remove(neuron)
|
|
|
+
|
|
|
+ if instantiated_neuron.pending_synapse:
|
|
|
+
|
|
|
+ cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
|
|
|
+
|
|
|
+
|
|
|
+ raise Synapse_LIFO
|
|
|
+
|
|
|
+
|
|
|
+ last_synapse_fifo_list .remove(matched_synapse)
|
|
|
+
|
|
|
+
|
|
|
+ cls.lifo_list.remove(last_synapse_fifo_list)
|
|
|
+ except Synapse_LIFO:
|
|
|
+ continue
|
|
|
+ raise Serialize
|
|
|
|
|
|
-
|
|
|
- while cls.lifo_list:
|
|
|
- logger.debug("[LIFOBuffer] number of synapse list to process: %s" % len(cls.lifo_list))
|
|
|
-
|
|
|
-
|
|
|
- cls.synapse_list_added_to_lifo = False
|
|
|
-
|
|
|
- last_synapse_fifo_list = cls.lifo_list[-1]
|
|
|
-
|
|
|
- while last_synapse_fifo_list:
|
|
|
-
|
|
|
- if cls.synapse_list_added_to_lifo:
|
|
|
- break
|
|
|
-
|
|
|
- matched_synapse = last_synapse_fifo_list[0]
|
|
|
-
|
|
|
-
|
|
|
- if matched_synapse not in cls.api_response.list_processed_matched_synapse:
|
|
|
- cls.api_response.list_processed_matched_synapse.append(matched_synapse)
|
|
|
-
|
|
|
- while matched_synapse.neuron_fifo_list:
|
|
|
- if cls.synapse_list_added_to_lifo:
|
|
|
- break
|
|
|
- logger.debug("[LIFOBuffer] number of neuron to process: %s" % len(matched_synapse.neuron_fifo_list))
|
|
|
-
|
|
|
- neuron = matched_synapse.neuron_fifo_list[0]
|
|
|
-
|
|
|
- if answer is not None:
|
|
|
- neuron.parameters["answer"] = answer
|
|
|
-
|
|
|
- answer = None
|
|
|
-
|
|
|
- neuron.parameters["is_api_call"] = is_api_call
|
|
|
-
|
|
|
- instantiated_neuron = NeuronLauncher.start_neuron(neuron=neuron,
|
|
|
- parameters_dict=matched_synapse.parameters)
|
|
|
-
|
|
|
-
|
|
|
- cls.api_response.status = "complete"
|
|
|
- if not instantiated_neuron.is_waiting_for_answer:
|
|
|
-
|
|
|
-
|
|
|
- matched_synapse.neuron_module_list.append(instantiated_neuron)
|
|
|
-
|
|
|
- matched_synapse.neuron_fifo_list.remove(neuron)
|
|
|
- else:
|
|
|
- print "Wait for answer mode"
|
|
|
- cls.api_response.status = "waiting_for_answer"
|
|
|
-
|
|
|
- will_be_returned = cls.api_response.serialize()
|
|
|
-
|
|
|
- cls.api_response = APIResponse()
|
|
|
- return will_be_returned
|
|
|
-
|
|
|
- if instantiated_neuron.pending_synapse:
|
|
|
-
|
|
|
- cls.add_synapse_list_to_lifo([instantiated_neuron.pending_synapse])
|
|
|
-
|
|
|
-
|
|
|
- cls.synapse_list_added_to_lifo = True
|
|
|
-
|
|
|
-
|
|
|
- if not cls.synapse_list_added_to_lifo:
|
|
|
-
|
|
|
- last_synapse_fifo_list .remove(matched_synapse)
|
|
|
-
|
|
|
-
|
|
|
- if not cls.synapse_list_added_to_lifo:
|
|
|
-
|
|
|
- cls.lifo_list.remove(last_synapse_fifo_list)
|
|
|
-
|
|
|
-
|
|
|
- will_be_returned = cls.api_response.serialize()
|
|
|
-
|
|
|
- cls.api_response = APIResponse()
|
|
|
- return will_be_returned
|
|
|
+ except Serialize:
|
|
|
+
|
|
|
+ will_be_returned = cls.api_response.serialize()
|
|
|
+
|
|
|
+ cls.api_response = APIResponse()
|
|
|
+ return will_be_returned
|
|
|
|
|
|
@classmethod
|
|
|
def clean(cls):
|