Threaded alternatives to smtplib?

Alex Jurkiewicz alex at bluebottle.net.au
Mon May 4 09:19:21 CEST 2009


Diez B. Roggisch wrote:
> Without more code, it's impossible to tell if there is anything 
> peculiar in your usage of the lib. Maybe you close your connections to 
> fast to see several open?

Here's the relevant stuff from my (python2.6) code:


CONCURRENCY = 3

def threadProcessRecipient():
    # Each thread has its own SMTP connection
    smtpserver = smtplib.SMTP(SMTPSERVER)
   
    # Each thread pulls a recipient entry from the queue to process and 
loops until the queue is empty.
    try:
        recipientData = recipientQueue.get_nowait()
    except Queue.Empty:
        recipientData = None
   
    while recipientData:
        message = prepareMessage()
        sendMail(senderEmail, recipientEmail, message, smtpserver)
       
        recipientQueue.task_done()
        try:
            recipientData = recipientQueue.get_nowait()
        except Queue.Empty:
            recipientData = None
   
    smtpserver.quit()

if __name__ == '__main__':
    THREADS = []
    for i in range(CONCURRENCY):
        THREADS.append(threading.Thread(target=threadProcessRecipient))
    for thread in THREADS:
        thread.run()
    for thread in THREADS:
        thread.join()



More information about the Python-list mailing list