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

后端 未结 6 2097
陌清茗
陌清茗 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:17

    I execute this in a cronjob

    from django.core.mail import EmailMessage
    from django.conf import settings
    import boto3
    import json
    
    sqs = boto3.resource('sqs', aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
             aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
             region_name=settings.AWS_REGION)
    
    queue = sqs.get_queue_by_name(QueueName='email')
    messages = queue.receive_messages(MaxNumberOfMessages=10, WaitTimeSeconds=1)
    
    while len(messages) > 0:
        for message in messages:
            mail_body = json.loads(message.body)
            print("E-mail sent to: %s" % mail_body['to'])
            email = EmailMessage(mail_body['subject'], mail_body['message'], to=[mail_body['to']])
            email.send()
            message.delete()
    
        messages = queue.receive_messages(MaxNumberOfMessages=10, WaitTimeSeconds=1)
    

提交回复
热议问题