def _handle_custom_message(self, message, response_message_id_supplier, response_consumer, result_consumer, workspace): if message.category == "my-nested-request-from-java": request_message_type = "first-request" response_payload = None else: message_type = message.get_header_field(AbstractTaskHandler.FIELD_KEY_MESSAGE_TYPE) if message_type == "first": self._response_string_payload += PayloadDecoder(message.payload).get_next_string() request_message_type = "second-request" response_payload = None elif message_type == "second": workspace.unregister_task_handler("my-nested-request-from-java") result_consumer(None) self._response_string_payload += PayloadDecoder(message.payload).get_next_string() request_message_type = AbstractTaskHandler.MESSAGE_TYPE_SUCCESS response_payload = PayloadEncoder().put_string(self._response_string_payload).payload else: error_message = "Message type '" + message_type + "' is not recognized. Illegal state." debug_util.debug_msg(error_message) raise RuntimeError(error_message) response_consumer[0] = Message(workspace._commands._messaging.create_next_message_id(), str(message.id), payload=response_payload, additional_options={AbstractTaskHandler.FIELD_KEY_MESSAGE_TYPE: request_message_type}) return True
def resolve_knime_url(self, url): """ :param url: File must exist as Java side tries to download remote files to a temporary location. """ payload = PayloadEncoder().put_string(url).payload return self.create_task( PythonCommands._ResolveKnimeUrlTaskHandler(), Message(self._messaging.create_next_message_id(), "resolve_knime_url", payload))
def trigger_request_from_python(workspace): class _TaskHandler(AbstractTaskHandler): def _handle_success_message(self, message): return PayloadDecoder(message.payload).get_next_string() my_task = workspace._commands.create_task(_TaskHandler(), Message(workspace._commands._messaging.create_next_message_id(), "caused-request-from-python")) return my_task.get()
def _create_response(request, response_message_id, success=True, response_payload=None, response_additional_options=None): options = response_additional_options or {} options[AbstractTaskHandler.FIELD_KEY_MESSAGE_TYPE] = ( AbstractTaskHandler.MESSAGE_TYPE_SUCCESS if success else AbstractTaskHandler.MESSAGE_TYPE_FAILURE) return Message(response_message_id, str(request.id), response_payload, options)
def receive(self): header_size = self._read_size() payload_size = self._read_size() header = self._read_data(header_size).decode('utf-8') if payload_size > 0: payload = self._read_data(payload_size) else: payload = None message = Message.create(header, payload) debug_msg("Python - Received message: " + str(message)) return message
def test_request_from_python_to_java(workspace): import debug_util debug_util.debug_msg("Entering test_request_from_python_to_java.") class _TaskHandler(AbstractTaskHandler): def _handle_success_message(self, message): import debug_util debug_util.debug_msg("Inside handler of Java response.") return PayloadDecoder(message.payload).get_next_string() my_task = workspace._commands.create_task(_TaskHandler(), Message( workspace._commands._messaging.create_next_message_id(), "my-request-from-python")) print(my_task.get()) # 'flush' keyword argument is not supported by Python 2. sys.stdout.flush()
class _ExecutionMonitor(object): _POISON_PILL = Message(-1, "", None, None) def __init__(self): self._close = Event() self._exception = None @property def poison_pill(self): return PythonKernel._ExecutionMonitor._POISON_PILL def create_message_queue(self, length): return PythonKernel._MonitoredMessageQueue(length, self) def report_close(self): self._close.set() def report_exception(self, exception, message=None): if self._exception is None and exception is not None: self._exception = exception try: if message is not None: error_message = message + " Cause: " + str(exception) else: error_message = "An exception occurred: " + str( exception) debug_msg(error_message, exc_info=True) except BaseException: pass self.report_close() def check_exception(self): if self._exception is not None: raise self._exception def wait_for_close(self): self._close.wait() self.check_exception()
class _ExecutionMonitor(object): # TODO: We should encapsulate this poison pill in a special "killable" queue implementation. _POISON_PILL = Message(-1, "", None, None) def __init__(self): self._close = Event() self._exception = None @property def poison_pill(self): return _ExecutionMonitor._POISON_PILL # TODO: This should be moved out of here. def create_message_queue(self, length): return _MonitoredMessageQueue(length, self) def report_close(self): self._close.set() def report_exception(self, exception, message=None): if self._exception is None and exception is not None: self._exception = exception try: if message is not None: error_message = message + " Cause: " + str(exception) else: error_message = "An exception occurred: " + str(exception) debug_msg(error_message, exc_info=True) except BaseException: pass self.report_close() def check_exception(self): if self._exception is not None: raise self._exception def wait_for_close(self): self._close.wait() self.check_exception()
def request_deserializer(self, id): payload = PayloadEncoder().put_string(id).payload return self.create_task(PythonCommands._DeserializerRequestTaskHandler(), Message(self._messaging.create_next_message_id(), "deserializer_request", payload))
def _create_message(self, message_category, payload=None): payload = PayloadEncoder().put_string(str(payload)).payload if payload is not None else None return Message(self._message_id_supplier(), self._reply_to, payload, {AbstractTaskHandler.FIELD_KEY_MESSAGE_TYPE: message_category})