Azure Batch [Python] - 反序列化错误:TaskAddParameter 没有属性 lower()

问题描述 投票:0回答:1

使用 python 组织批处理任务,其中容器接收文件路径列表,拉取文件并将它们传递到容器。

启动任务时出现以下错误:

2024-04-22T08:48:31.937977+0100 | ERROR    | Unexpected error when adding tasks: 
('Unable to build a model: ("Unable to deserialize to object: type, AttributeError: \'TaskAddParameter\' object has no attribute \'lower\'", 
AttributeError("\'TaskAddParameter\' object has no attribute \'lower\'")), 
DeserializationError: ("Unable to deserialize to object: type, AttributeError: \'TaskAddParameter\' object has no attribute \'lower\'", 
AttributeError("\'TaskAddParameter\' object has no attribute \'lower\'"))', 
DeserializationError("Unable to deserialize to object: type, 
AttributeError: 'TaskAddParameter' object has no attribute 'lower'", 
AttributeError("'TaskAddParameter' object has no attribute 'lower'")))

我用于创建池和作业等的其余代码运行良好。

这是我的代码中负责创建任务的部分:

    def create_task(self):

        files_to_process = []

        registry_server = self.config.REGISTRY_LOGIN_SERVER
        container_name = self.args.container_name
        container_version = self.args.container_version

        if os.path.isdir(self.args.files):
            for file in os.listdir(self.args.files):
                if os.path.isfile(os.path.join(self.args.files, file)):
                    files_to_process.append(os.path.join(self.args.files, file))
        else:
            files_to_process.append(self.args.files)

        tasks = []

        for i, file_name in enumerate(files_to_process, start=1):

            task_name = f"{self.args.task_prefix}-{self.config.REGISTRY_NAME}-{str(i)}".lower()

            logger.debug("Adding task: " + task_name)

            task_container_settings = TaskContainerSettings(
                image_name=f"{registry_server}/{container_name}:{container_version}", # This is the container image
                container_run_options=f"--env FILES={file_name}"
            )

            if not isinstance(task_name, str) or not isinstance(file_name, str):
                logger.error(f"Type error in task parameters: {task_name}, {file_name}")
                continue  # Skip this iteration if there's a type mismatch

            task = TaskAddParameter(
                id=task_name,
                command_line="/bin/bash -c 'echo Starting task; ./entrypoint.sh'",
                container_settings=task_container_settings
            )

            logger.debug("TaskAddParameter details: ID={}, CommandLine={}, ContainerSettings={}".format(
                task.id, task.command_line, task.container_settings
            ))

            tasks.append(task)

            try:
                logger.info(f"Adding {len(tasks)} tasks to job '{self.last_job_name}'...")
                self.batch_client.task.add(self.last_job_name, tasks)
                logger.info(f"Tasks added to job '{self.last_job_name}'")

            except BatchErrorException as e:
                logger.error(f"Failed to add tasks to job '{self.last_job_name}': {e}")
                exit()

            except Exception as e:
                logger.error(f"Unexpected error when adding tasks: {e}")
                exit()

        pass

这是下面代码片段的控制台输出:

2024-04-22T08:48:31.856327+0100 | INFO     | Attempting to create job '2024-04-22-08-48-29-test-'...
2024-04-22T08:48:31.935065+0100 | INFO     | Created job '2024-04-22-08-48-29-test-'
2024-04-22T08:48:31.936087+0100 | DEBUG    | Adding task: 2024-04-22-08-48-29-test-1
2024-04-22T08:48:31.936981+0100 | DEBUG    | TaskAddParameter details: ID=2024-04-22-08-48-29-test-1, CommandLine=/bin/bash -c 'echo Starting task; ./entrypoint.sh', ContainerSettings={'additional_properties': {}, 'container_run_options': '--env FILES=.\\test-files\\bc_train_batch-0.txt', 'image_name': 'XXX.azurecr.io/sequencer:latest', 'registry': None, 'working_directory': None}
2024-04-22T08:48:31.936981+0100 | INFO     | Adding 1 tasks to job '2024-04-22-08-48-29-test-'...
2024-04-22T08:48:31.937977+0100 | ERROR    | Unexpected error when adding tasks: ('Unable to build a model: ("Unable to deserialize to object: type, AttributeError: \'TaskAddParameter\' object has no attribute \'lower\'", AttributeError("\'TaskAddParameter\' object has no attribute \'lower\'")), DeserializationError: ("Unable to deserialize to object: type, AttributeError: \'TaskAddParameter\' object has no attribute \'lower\'", AttributeError("\'TaskAddParameter\' object has no attribute \'lower\'"))', DeserializationError("Unable to deserialize to object: type, AttributeError: 'TaskAddParameter' object has no attribute 'lower'", AttributeError("'TaskAddParameter' object has no attribute 'lower'")))

我查看了 msrest 的相关问题(如下所列),但无法解决问题

Azure SDK 文档

Git 中心问题#1

Git 中心问题#2

python azure azure-batch
1个回答
0
投票

Azure Batch [Python] - 反序列化错误:TaskAddParameter 没有属性 lower()

出现上述错误是由于数据在反序列化时,可能包含意外的值或类型,或者可能不符合预期的结构。尝试使用下面的代码,它在开始时包含日志记录初始化,以保证您在整个执行过程中进行日志记录。为了监视程序的流程并观察对象在不同时间点的状况,需要使用调试语句。

class TaskManager:
    def __init__(self, batchClient, config, args):
        self.batch_client = batchClient
        self.config = config
        self.args = args
        self.lastJobName = None

    def create_task(self):
        logger = logging.getLogger(__module1__)

        files_to_process = []

        registry_server = self.config.REGISTRY_LOGIN_SERVER
        containerName = self.args.containerName
        containerVersion = self.args.containerVersion

        if os.path.isdir(self.args.files):
            for file1 in os.listdir(self.args.files):
                if os.path.isfile(os.path.join(self.args.files, file1)):
                    files_to_process.append(os.path.join(self.args.files, file1))
        else:
            files_to_process.append(self.args.files)

        for i, file1 in enumerate(files_to_process, start=1):
            task1 = f"{self.args.task_prefix}-{self.config.REGISTRY_NAME}-{str(i)}".lower()

            logger.debug("Adding task: " + task1)

            task_container_settings = TaskContainerSettings(
                image1=f"{registry_server}/{containerName}:{containerVersion}",
                container_run_options=f"--env FILES={file1}"
            )

            try:
                task = TaskAddParameter(
                    id=task1,
                    command_line="/bin/bash -c 'echo Starting task; ./entrypoint.sh'",
                    container_settings=task_container_settings
                )

                logger.debug("TaskAddParameter details: ID={}, CommandLine={}, ContainerSettings={}".format(
                    task.id, task.command_line, task.container_settings
                ))

                logger.info(f"Adding task {task.id} to job '{self.lastJobName}'...")
                self.batch_client.task.add(self.lastJobName, task)
                logger.info(f"Task {task.id} added to job '{self.lastJobName}'")

            except BatchErrorException as e:
                logger.error(f"Failed to add tasks to job '{self.lastJobName}': {e}")
                exit()

            except Exception as e:
                logger.error(f"Unexpected error when adding tasks: {e}")
                logger.exception("Exception occurred")
                exit()

输出:

INFO     | Attempting to create job '2024-05-03-12-00-00-test-'
INFO     | Created job '2024-05-03-12-00-00-test-'
DEBUG    | Adding task: 2024-05-03-12-00-00-test-1
DEBUG    | TaskAddParameter details: ID=2024-05-03-12-00-00-test-1, CommandLine=/bin/bash -c 'echo Starting task; ./entrypoint.sh', ContainerSettings={'additional_properties': {}, 'container_run_options': '--env FILES=./test-files/bc_train_batch-0.txt', 'image1': 'XXX.azurecr.io/sequencer:latest', 'registry': None, 'working_directory': None}
INFO     | Adding 1 tasks to job '2024-05-03-12-00-00-test-'
INFO     | Tasks added to job '2024-05-03-12-00-00-test-'
  • 查看此文档以获取更多信息。
© www.soinside.com 2019 - 2024. All rights reserved.