Python/Django: sending emails in the background

后端 未结 3 377
盖世英雄少女心
盖世英雄少女心 2021-02-04 05:04

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 wi

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 05:30

    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()
    

提交回复
热议问题