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
+8 -11
View File
@@ -25,8 +25,7 @@ def getResources(region_name: str) -> list[list[str | int]]:
for t in table_names:
resp2 = client.describe_table(TableName=t)
i = resp2['Table']
if i:
return_data.append([i['TableName'], i['TableStatus'], i['TableSizeBytes'], i['ItemCount'], region_name])
return_data.append([i['TableName'], i['TableStatus'], i['TableSizeBytes'], i['ItemCount'], region_name])
return return_data
# Main function
@@ -35,16 +34,14 @@ def main() -> None:
wb = load_workbook('aws-inventory.xlsx')
ws = wb.create_sheet("DynamoDB")
final_data = []
ws.append(["TableName", "TableStatus", "TableSizeBytes", "ItemCount", "Region"])
with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor:
futures = [executor.submit(getResources, 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')