Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send multiple messages in one connection #19

Merged
merged 3 commits into from Oct 24, 2022

Conversation

cha798215
Copy link
Contributor

As Django document said, establishing and closing an SMTP connection (or any other network connection, for that matter) is an expensive process. So we should reuse an SMTP connection to send multiple emails, rather than creating and destroying a connection every single email.

This PR change the strategy of sending email. It is no longer sending a task for each email message one by one. It slice the email messages to many of chunks and send a task for each chunk. You can set your own DJCELERY_SES_CHUNK_SIZE to control how many email messages you want to send by every single connection.

Because of async tasks, it can only get ids of the tasks, instead of the real result of sending mails. So send_messages will return the length of the argument email_messages.

Comment on lines 22 to 28
pages, remainder = divmod(len(email_messages), CHUNK_SIZE)
if remainder != 0:
pages += 1

for page in range(pages):
offset = CHUNK_SIZE * page
email_messages_chunk = email_messages[offset: offset + CHUNK_SIZE]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pages, remainder = divmod(len(email_messages), CHUNK_SIZE)
if remainder != 0:
pages += 1
for page in range(pages):
offset = CHUNK_SIZE * page
email_messages_chunk = email_messages[offset: offset + CHUNK_SIZE]
for index in range(0, len(email_messages), CHUNK_SIZE):
email_messages_chunk = email_messages[index: index + CHUNK_SIZE]

@cha798215 cha798215 merged commit ca756a7 into master Oct 24, 2022
@cha798215 cha798215 deleted the enhance/send-multiple-messages-in-one-connection branch October 24, 2022 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants