How to get all messages in Amazon SQS queue using boto library in Python?

后端 未结 6 2091
陌清茗
陌清茗 2021-02-05 10:49

I\'m working on an application whose workflow is managed by passing messages in SQS, using boto.

My SQS queue is growing gradually, and I have no way to check how many e

6条回答
  •  孤独总比滥情好
    2021-02-05 11:15

    Something like the code below should do the trick. Sorry it's in C#, but it shouldn't be hard to convert to python. The dictionary is used to weed out the duplicates.

        public Dictionary GetAllMessages(int pollSeconds)
        {
            var msgs = new Dictionary();
            var end = DateTime.Now.AddSeconds(pollSeconds);
    
            while (DateTime.Now <= end)
            {
                var request = new ReceiveMessageRequest(Url);
                request.MaxNumberOfMessages = 10;
    
                var response = GetClient().ReceiveMessage(request);
    
                foreach (var msg in response.Messages)
                {
                    if (!msgs.ContainsKey(msg.MessageId))
                    {
                        msgs.Add(msg.MessageId, msg);
                    }
                }
            }
    
            return msgs;
        }
    

提交回复
热议问题