Django: Best way to send email in background?

試著忘記壹切 提交于 2019-12-19 06:57:33

问题


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

Any ideas for what is the best way to do this?

I have read about celery, but it seems like a lot of steps to set it up: http://markliu.me/2011/sep/29/django-celery-on-webfaction-using-rabbitmq/ That's Ok, but I'd like to know that this is the way to go before trying it out.

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

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

Others that you have experience with?


回答1:


Let's make a simple overview of possible solutions:

  1. Threads are bad solution - because they are live only before your response is not send.

  2. Celery - is standard way, it's easy to add to django ( just see one of a lot of tutorial about django-celery, for your task using database as broker is enough)

  3. Cron jobs - is not really good programmer's way because your code will store in your repo, and in system crontab. SO everytime you should think about that.

  4. Other way is using something like Eventlet or Gevent. Green threads will live in work in idle, and for your standard task - is very easy to add. Disadvantages: - is you should understand a lot about greenlets, you should be careful for error catching in greenlet.

I recommend to use, Celery because, it's easy to add it now, a lot of tutorial and documentation. Also it's will easy grow up with your application.




回答2:


I would strongly consider using Celery. It isn't as complicated as it appears and is a great tool for doing arbitrary asynchronous tasks. However, there is an easy way to do background e-mails using Django and a standard cron job.

First, create a Django model to save the emails to send.

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

Next, create a Django admin command to send unsent emails. See the Django documentation for more 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()

Then you can schedule this command using a cron job. However, I personally would rather use Celery or something similar. It is a little more work upfront, but pays off in the long-term.




回答3:


I'd probably go with Python RQ. This is a more minimal alternative to Celery and is very easy to set up and to use. You need redis for it though.




回答4:


Depends on the scale of your application. If it is plan to stay minimal and small, threads work fine and cronjobs will do too. But you will soon want to delegate a lot of the work to the background to speed up the request/response time. SO yes, setting up celery+rabbit on webfaction is a semi-complex task(been there) but on the long run you will save time and effort.



来源:https://stackoverflow.com/questions/13567160/django-best-way-to-send-email-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!