1

i want to send multiple emails in a view. what i need is, that view sends the responcse back and send emails in the background.

when i run

return EmailMultiAlternatives(subject,
                             t,
                             sender,
                             recipients,
                             bcc=bcc).send()

the view does not send the response until it sends an email or two or whatever.

i want to ask why

    yield EmailMultiAlternatives(subject,
                             t,
                             sender,
                             recipients,
                             bcc=bcc).send()

instruction not send my email?? why yield is not behaving like i want it to?

A.J.
  • 8,557
  • 11
  • 61
  • 89

1 Answers1

1

Django framework (unlike Tornado, or Twisted) works in a synchronous/blocking mode, the view won't return you the response until it did everything.

If you don't want to wait for emails to be sent, you should take a look at celery and django-celery packages. Define a task, put it on a queue from the view, return the response and let the worker do the job "in the background".

Also, see:

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks for your quick response. i am looking at them now. – A.J. Aug 02 '13 at 12:58
  • Yes!, Celery is great, but as well you can just make AJAX requests to send mails what is simpler. Just make a url for sending e-mail and view where you will actually send mail and send simple response which you can show in html. – adamr Aug 02 '13 at 12:59
  • like in python Scrapy framework, the callback method can yield any request. – A.J. Aug 02 '13 at 12:59