Django: Best way to send email in the background?

I am sending an email with Django (using Webfaction). This, however, is rather slow, so I would like to send an email in the background, returning a response to the user, even if the email has not yet been sent.

Any ideas for a better way to do this?

I read about celery, but it seems to me that he did a lot to set it up: http://markliu.me/2011/sep/29/django-celery-on-webfaction-using-rabbitmq/ That's good, but I'm ' d would like to know what is the way before trying it.

What about threads? http://www.artfulcode.net/articles/threading-django/

Or cron jobs? http://docs.webfaction.com/software/general.html

Others with whom you have experience?

+6
source share
4 answers

Give a simple overview of the possible solutions:

  • Threads are a bad decision - because they live only until your answer is sent.

  • Celery is a standard way, it is easy to add to django (just see one of the many lessons about django-celery, for your task using the database as a broker is enough)

  • Cron's work is actually not a very good programmer, because your code will be stored in your repo and in the crontab system. SO every time you have to think about it.

  • Another way is using something like Eventlet or Gevent. Green threads will work in standby mode, and for your standard task it is very easy to add. Disadvantages: - you have to understand a lot about greens, you have to be careful when detecting errors in the greenlet.

I recommend using celery, because it is easy to add now, a lot of tutorial and documentation. It will also grow easily with your application.

+4
source

I would probably go with Python RQ . This is a more minimal alternative to celery and is very easy to set up and use. You need redis for this though.

+1
source

I would think a lot about using Celery. It is not as difficult as it seems, and is an excellent tool for performing arbitrary asynchronous tasks. However, there is an easy way to do background emails using Django and the standard cron job.

First create a Django model to preserve emails you send.

class EmailsToSend(models.Model): email = models.Email... . . . 

Then create a Django admin command to send unsent emails. See the Django documentation for details on how to do this. This code gives you the basic idea.

 class Command(BaseCommand): def handle(self, *args, **options): emails = EmailsToSend.objects.all() for email in emails: send_my_email(email) email.delete() 

You can then schedule this command using the cron job. However, I personally would prefer to use celery or something similar. This is a bit more work, but pays off in the long run.

+1
source

Depends on the scale of your application. If he plans to stay minimal and small, the threads work fine, and cronjobs will do the same. But you will soon want to delegate most of the work to the background to speed up the request / response time. So yes, setting celery + rabbit to webfaction is a difficult task (was there), but in the long run you will save time and effort.

0
source

All Articles