0

I am having troubles optimizing my python email sender function that uses flask-mail.

from flask_mail import Message, Mail

def send_email_msg():
    with mail.connect() as emailConn:
         with app.app_context():
              for msg in msgList :

              try:
                  emailConn.send(msg)
                  return 1

              except smtplib.SMTPException, e:
                  return 0

I have tried using asynchronous calls to send the emails via threads but the solution was not very feasible for me as the error rate was relatively high. In addition I have removed the restriction for maximum number of emails that can be send per connection as well.

Currently, it takes about 1.4s to send a single email (with processing time about 1.6s). Ideally I would like to send a email within 0.6 to 0.8s.

Please advice me the possible libraries or solutions that I can use to achieve this. Since I am using amazon ses as my mail server, I have also tried out boto.ses with negligible performance difference. Please suggest to me the relevant libraries that I can use. Alternatively, are there any APIs which allow me to send multiple emails in one call (say I render 10 template emails but send them out at one go)?

Cheers.

Mufiz
  • 9
  • 3
  • What's the reason for the app_context() wrapper? – Mark R. Feb 17 '15 at 10:05
  • "Note that we needed to create an app_context to send the email. Recent releases of Flask-Mail require this. An application context is created automatically when a request is handled by Flask. Since we are not inside a request we have to create the context by hand just so that Flask-Mail can do its job." - http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support – Mufiz Feb 17 '15 at 10:15
  • How are you measuring the timings? – Mark R. Feb 17 '15 at 10:22
  • I using python timeit and I record the start time before the function call and the end time after the function call, the difference gives me the time taken. – Mufiz Feb 17 '15 at 10:25

0 Answers0