From 01369550de11e051c6f0ba3d1429fd7ea292cf0b Mon Sep 17 00:00:00 2001 From: Evan Carlin Date: Tue, 29 Oct 2024 20:07:55 -0600 Subject: [PATCH] Fix #988: Remove async example The async example never actually worked in an async way. It tricked users into thinking they were using async code but in reality the sendgrid_client.send call (which is what was waiting on I/O) was still blocking. In addition, asyncio.async (used in the example) was deprecated in Python 3.7 and removed in Python 3.10. --- use_cases/asynchronous_mail_send.md | 83 ----------------------------- 1 file changed, 83 deletions(-) delete mode 100644 use_cases/asynchronous_mail_send.md diff --git a/use_cases/asynchronous_mail_send.md b/use_cases/asynchronous_mail_send.md deleted file mode 100644 index de38dc751..000000000 --- a/use_cases/asynchronous_mail_send.md +++ /dev/null @@ -1,83 +0,0 @@ -# Asynchronous Mail Send - -## Using `asyncio` (3.5+) - -The built-in `asyncio` library can be used to send email in a non-blocking manner. `asyncio` helps us execute mail sending in a separate context, allowing us to continue the execution of business logic without waiting for all our emails to send first. - -```python -from sendgrid import SendGridAPIClient -from sendgrid.helpers.mail import Content, Mail, From, To, Mail -import os -import asyncio - - -sendgrid_client = SendGridAPIClient( - api_key=os.environ.get('SENDGRID_API_KEY')) - -from_email = From("test@example.com") -to_email = To("test1@example.com") - -plain_text_content = Content("text/plain", "This is asynchronous sending test.") -html_content = Content("text/html", "This is asynchronous sending test.") - -# instantiate `sendgrid.helpers.mail.Mail` objects -em1 = Mail(from_email, to_email, "Message #1", content) -em2 = Mail(from_email, to_email, "Message #2", content) -em3 = Mail(from_email, to_email, "Message #3", content) -em4 = Mail(from_email, to_email, "Message #4", content) -em5 = Mail(from_email, to_email, "Message #5", content) -em6 = Mail(from_email, to_email, "Message #6", content) -em7 = Mail(from_email, to_email, "Message #7", content) -em8 = Mail(from_email, to_email, "Message #8", content) -em9 = Mail(from_email, to_email, "Message #9", content) -em10 = Mail(from_email, to_email, "Message #10", content) - - -ems = [em1, em2, em3, em4, em5, em6, em7, em8, em9, em10] - - -async def send_email(n, email): - ''' - send_mail wraps Twilio SendGrid's API client, and makes a POST request to - the api/v3/mail/send endpoint with `email`. - Args: - email: single mail object. - ''' - try: - response = sendgrid_client.send(request_body=email) - if response.status_code < 300: - print("Email #{} processed".format(n), response.body, response.status_code) - except urllib.error.HTTPError as e: - e.read() - - -@asyncio.coroutine -def send_many(emails, cb): - ''' - send_many creates a number of non-blocking tasks (to send email) - that will run on the existing event loop. Due to non-blocking nature, - you can include a callback that will run after all tasks have been queued. - - Args: - emails: contains any # of `sendgrid.helpers.mail.Mail`. - cb: a function that will execute immediately. - ''' - print("START - sending emails ...") - for n, em in enumerate(emails): - asyncio.async(send_email(n, em)) - print("END - returning control...") - cb() - - -def sample_cb(): - print("Executing callback now...") - for i in range(0, 100): - print(i) - return - - -if __name__ == "__main__": - loop = asyncio.get_event_loop() - task = asyncio.async(send_many(ems, sample_cb)) - loop.run_until_complete(task) -``` \ No newline at end of file