[Django]-Send_mass_mail in background django

3πŸ‘

You need to move the tasks to the background (so they don’t block the web process). One of the most popular ways to do this is to use a messaging/task queue.

Celery is one of the most popular distributed task queues, and coupled with the django-celery application makes this trivial.

First you need to setup celery (which is as simple as pip install -U celery); and one of the many messaging brokers that it supports. The most popular one is RabbitMQ; but for quick and dirty set ups you can also use your existing database as a message broker.

Finally, since this is a common problem solved by celery+django, there is django-celery-email which takes care of the rest.

3πŸ‘

You might want to look into the django-mailer project, which encapsulates this functionality – it does this via crons, rather than using a task queue. I’ve been using it for a while with good results.

1πŸ‘

You can send mails in separate thread, for example:

t = threading.Thread(target=send_mass_email,
            args=[messages],
            kwargs={'fail_silently': True})
t.setDaemon(True)
t.start()

or just use cron and django management commands =)
https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

0πŸ‘

You can make it a management command and then setup a periodic cron job to send out the emails refer management command docs

Leave a comment