简体   繁体   中英

Python/Django: sending emails in the background

Imagine a situation in which a user performs an action on a website and admins are notified. Imagine there are 20 admins to notify. By using normal methods for sending emails with Django the user will have to wait until all the emails are sent before being able to proceed.

How can I send all the emails in a separate process so the user doesn't have to wait? Is it possible?

celery用作任务队列,并使用django-celery-email(这是Django电子邮件后端),用于将电子邮件发送给celery任务。

Another option is django-mailer. It queues up mail in a database table and then you use a cron job to send them.

https://github.com/pinax/django-mailer

A thread may be a possible solution. I use threads intensively in my application for haevy tasks.

# This Python file uses the following encoding: utf-8

#threading
from threading import Thread

...

class afegeixThread(Thread):

    def __init__ (self,usuari, parameter=None):
        Thread.__init__(self)
        self.parameter = parameter
        ...

    def run(self):        
        errors = []
        try:
             if self.paramenter:
                   ....
        except Exception, e:                
             ...
...

n = afegeixThread( 'p1' )
n.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM