在AWS批处理作业中,长时间的time.sleep()可能会导致作业失败。一种解决方案是,将time.sleep()重构为较短的时间段,并在每个时间段后检查作业的状态。可以使用AWS的boto3库来实现这个检查,具体代码如下:
import boto3
import time
# Create a client using the default AWS configuration.
client = boto3.client('batch')
# Function to check job status
def check_job_status(job_id):
response = client.describe_jobs(jobs=[job_id])
status = response['jobs'][0]['status']
return status
# Main function
def main():
job_id = # specify the job ID
wait_time = 30 # Set the wait time for each iteration
while True:
time.sleep(wait_time)
status = check_job_status(job_id)
if status == 'SUCCEEDED':
print('Job Succeeded!')
break
elif status == 'FAILED':
print('Job Failed!')
break
else:
print('Job Status: ' + status)
if __name__ == '__main__':
main()
该代码将time.sleep()重构为一个while循环,使用check_job_status()函数来检查作业的状态。每个时间段等待完成后,将打印作业状态,并检查是否已完成或失败。如果作业已成功完成,程序将退出while循环并打印一个成功消息。如果作业失败,程序将退出while循环并打印一个失败消息。