Created
September 20, 2021 08:23
-
-
Save mark-mishyn/ec5539165e71346f592e48601c288b97 to your computer and use it in GitHub Desktop.
Send SQS messages in batch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BatchSendToSQS: | |
""" | |
>>> with BatchSendToSQS(someQueue, batch_size=10) as sqs_batcher: | |
>>> sqs_batcher.add_msg({'data': 'booba'}) | |
>>> ... | |
>>> sqs_batcher.add_msg({'data': 'piu piu'}) | |
""" | |
def __init__(self, queue: "boto3.sqs.Queue", batch_size: int): | |
self.queue = queue | |
self.messages_to_send = [] | |
self.batch_size = batch_size | |
def add_msg(self, msg): | |
self.messages_to_send.append(msg) | |
if len(self.messages_to_send) == self.batch_size: | |
self.queue.send_messages(Entries=self.messages_to_send) | |
self.messages_to_send.clear() | |
def __enter__(self): | |
return self | |
def __exit__(self, *exc): | |
# send last batch of messages | |
if self.messages_to_send: | |
self.queue.send_messages(Entries=self.messages_to_send) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment