def execute_command(self, command, args, method):
        """
            For any given command and arguments, the function connects to
            the moirai FIFO and reads the ouput via api FIFO
        """
        if self.moirai_fifo_fd is None:
            try:
                self.moirai_fifo_fd = os.open(
                    self.api_config['moirai_input_fifo'],
                    os.O_WRONLY | os.O_NONBLOCK)
                self.moirai_fifo_file = os.fdopen(self.moirai_fifo_fd, 'w')
            except:
                self.moirai_fifo_fd = None
                return "Unable to connect to Moirai Service"
        self.command_id = self.command_id + 1
        command_map = {ApiConstants.FIFO_ENDPOINT_KEY: self.api_fifo_path,
                       ApiConstants.COMMAND_ID_KEY: self.command_id,
                       ApiConstants.COMMAND_KEY: command,
                       ApiConstants.ARGS_KEY: args,
                       ApiConstants.METHOD_KEY: method}
        command_string = json.dumps(command_map) + '\n'
        try:
            self.moirai_fifo_file.write(command_string)
            self.moirai_fifo_file.flush()
        except IOError:
            op_msg = ("Unable to communicate with moirai")
            print "LOG: " + op_msg
            return op_msg
        except OSError:
            self.moirai_fifo_fd = None
            op_msg = ("Unable to communicate with moirai")
            print "LOG: " + op_msg
            return op_msg

        # Read from api fifo
        start_time = time.time()
        output_string = ''
        while output_string == '':
            try:
                output_string = self.api_fifo_file.read()
            except IOError:
                pass
            current_time = time.time()
            if current_time - start_time > ApiConstants.READ_TIMEOUT_SECS:
                op_msg = ("No message recieved from Moirai")
                print "LOG: " + op_msg
                return op_msg
        try:
            output_obj = json.loads(output_string)
        except ValueError:
            op_msg = "Recieved object is not a json object"
            print "LOG: " + op_msg
            return op_msg
        if output_obj[ApiConstants.COMMAND_ID_KEY] != self.command_id:
            op_msg = ("Recieved output with incorrect command id")
            print "LOG: " + op_msg
            return op_msg
        return output_obj[ApiConstants.COMMAND_OUTPUT_KEY]
dataset/ETHPy150Open linkedin/simoorg/src/simoorg/Api/MoiraiApiServer.py/MoiraiApiServer.execute_command/original