refector: optimized concurrent.futures

This commit is contained in:
KenF
2026-01-23 08:20:08 +08:00
parent 675f0f6ef3
commit eb68f146ed
13 changed files with 115 additions and 154 deletions
+9 -12
View File
@@ -16,13 +16,12 @@ def getRegions(all_regions=False):
response = ec2.describe_regions(AllRegions=all_regions)
return [region['RegionName'] for region in response['Regions']]
def printResources(region_name: str) -> list[list[str | int]]:
def getResources(region_name: str) -> list[list[str | int]]:
return_data = []
client = boto3.client('emr', region_name=region_name)
response = client.list_clusters()
for i in response['Clusters']:
if i:
return_data.append([i['Name'], i['Status'], i['NormalizedInstanceHours'], region_name])
return_data.append([i['Name'], i['Status'], i['NormalizedInstanceHours'], region_name])
return return_data
# Main function
@@ -31,16 +30,14 @@ def main() -> None:
wb = load_workbook('aws-inventory.xlsx')
ws = wb.create_sheet("EMR")
final_data = []
ws.append(["Name", "Status", "NormalizedInstanceHours", "Region"])
with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor:
futures = [executor.submit(printResources, region_name=r) for r in getRegions()]
for future in concurrent.futures.as_completed(futures):
region_data = future.result()
final_data.extend(region_data)
for row in final_data:
ws.append(row)
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
results = executor.map(getResources, getRegions())
for region_rows in results:
# append to worksheet only if resoruces are found in the region
if region_rows:
for row in region_rows:
ws.append(row)
wb.save('aws-inventory.xlsx')