Skip to main content

use concurrently to get the queue attributes faster

ID
9da95f3
date
2023-05-17 15:48:55+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
d501995
message
use concurrently to get the queue attributes faster
changed files
1 file, 14 additions, 5 deletions

Changed files

aws/sqs_stats.py (3623) → aws/sqs_stats.py (3885)

diff --git a/aws/sqs_stats.py b/aws/sqs_stats.py
index e052d9e..b923f49 100755
--- a/aws/sqs_stats.py
+++ b/aws/sqs_stats.py
@@ -15,6 +15,7 @@ e.g. `calm-windows` and `calm-windows_dlq`.
 """
 
 import collections
+import os
 import sys
 
 import boto3
@@ -23,6 +24,11 @@ import termcolor
 
 from _common import create_link_text
 
+# https://github.com/alexwlchan/concurrently
+sys.path.append(os.path.join(os.environ["HOME"], "repos", "concurrently"))
+
+from concurrently import concurrently
+
 
 def list_queue_urls_in_account(sess, *, prefixes):
     """
@@ -49,12 +55,15 @@ def get_queue_stats(sess, *, queue_urls):
         "ApproximateNumberOfMessagesDelayed",
     ]
 
-    queue_responses = {
-        q_url: sqs_client.get_queue_attributes(
+    queue_responses = {}
+
+    for q_url, q_resp in concurrently(
+        handler=lambda q_url: sqs_client.get_queue_attributes(
             QueueUrl=q_url, AttributeNames=attribute_names
-        )
-        for q_url in queue_urls
-    }
+        ),
+        inputs=queue_urls
+    ):
+        queue_responses[q_url] = q_resp
 
     return {
         q_url: sum(int(resp["Attributes"][attr]) for attr in attribute_names)