From 4691c27c7f2fde798b35300b24b320b1cf292b9f Mon Sep 17 00:00:00 2001 From: chen524 Date: Wed, 18 Oct 2023 03:30:18 +0000 Subject: [PATCH] update examples/python/create-rankfile.py. Signed-off-by: chen524 --- examples/python/create-rankfile.py | 31 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/examples/python/create-rankfile.py b/examples/python/create-rankfile.py index d7f5eab..b21e2ba 100644 --- a/examples/python/create-rankfile.py +++ b/examples/python/create-rankfile.py @@ -1,21 +1,26 @@ def generate_rankfile(hostfile_path): rankfile_content = "" rank_id = 0 - with open(hostfile_path, "r") as hostfile: - hosts = hostfile.readlines() - num_hosts = len(hosts) - ranks_per_host = 64 - for host_id, host in enumerate(hosts): - hostname = host.strip() - for rank in range(ranks_per_host): - rank_content = f"rank {rank_id}={hostname} slot=0-63" - rankfile_content += rank_content + '\n' - rank_id = rank_id + 1 + try: + with open(hostfile_path, "r") as hostfile: + hosts = hostfile.readlines() + num_hosts = len(hosts) + ranks_per_host = 64 + for host_id, host in enumerate(hosts): + hostname = host.strip() + for rank in range(ranks_per_host): + rank_content = f"rank {rank_id}={hostname} slot=0-63" + rankfile_content += rank_content + '\n' + rank_id = rank_id + 1 - with open("rankfile", "w") as rankfile: - rankfile.writelines(rankfile_content) + with open("rankfile", "w") as rankfile: + rankfile.writelines(rankfile_content) - print("Rankfile generated successfully.") + print("Rankfile generated successfully.") + except FileNotFoundError: + print(f"Error: File {hostfile_path} not found.") + except Exception as e: + print(f"An error occurred: {str(e)}") # 请将hostfile_path变量替换为你自己的hostfile文件路径 hostfile_path = "hostfile.2" -- Gitee