Simple programme - Just want to know whether this is correct way of coding
Piet van Oostrum
piet at cs.uu.nl
Fri May 8 05:12:21 EDT 2009
>>>>> guptha <gjango.py at gmail.com> (g) wrote:
>g> Hi group,
>g> This is my first programme in python ,I need to know whether my code
>g> is in the right path of performance
>g> I wrote a code using multithreading to send mails
>g> FROM = '....com'
>g> SUBJECT = 'This is the subject'
>g> MSGBODY = 'This the body of the message '
>g> MAILSERVER = 'mail....com'
>g> port = 25
>g> username = 'username'
>g> password = 'pass'
>g> # trim the strings of any leading or trailing spaces
>g> FROM = FROM.strip()
>g> SUBJECT = SUBJECT.strip()
>g> MSGBODY = MSGBODY.strip()
>g> MAILSERVER = MAILSERVER.strip()
>g> username = username.strip()
>g> password = password.strip()
>g> # set up email parameters
>g> msg = MIMEMultipart()
>g> msg['From'] = FROM
>g> msg['Subject'] = SUBJECT
>g> print "Starting Multi Thread Method"
>g> class MyThread(Thread):
>g> def __init__(self, site, FROM, MSGBODY):
>g> self.site = site
>g> def run(self):
>g> #Connect to server
>g> print 'Connecting to mail server ', MAILSERVER
>g> s = smtplib.SMTP(MAILSERVER,port)
>g> print 'connected'
>g> print 'ERROR: Unable to connect to mail server', sys.exc_info ()
>g> #login to server
>g> if password <> '':
>g> print 'Logging into mail server'
>g> print 'ERROR: Unable to login to mail server', MAILSERVER
>g> print 'Please recheck your password'
>g> print "running for %s " %self.site
>g> print s
>g> s.sendmail(self.FROM, self.site, self.MSGBODY)
>g> print "from %s" %self.FROM
>g> print "msg %s" %self.MSGBODY
>g> print "Emailed for site %s" %self.site
>g> a= time.time()
>g> threads = 
>g> for site in listTo:
>g> T = MyThread(site,FROM,MSGBODY)
>g> for i in threads:
>g> print "Took %s seconds" %str(time.time()-a)
Tuesday this same program with some minor differences was posted by
gganesh <ganesh.gbg at gmail.com>. There was a discussion about that. So
did you copied that program with the suggested changes? I don't think it
is nice to have two parallel discussions on the same subject. And
copying without giving credits is not considered nice behaviour either.
Usually it is called plagiarism.
>g> The code Works fine ,but I doubt about the performance issue ,My
>g> intention is to send mails concurrently to large number of mail.
>g> 1.For every mail id i send It creates a new SMTP object,in case, if i
>g> send to 1000 or more ids
>g> a) It obliviously creates that much SMPT connections ,is this a
>g> right approach .
For such a big number of mails this is not the right approach. First
your operating system may not like it to have such a large number of
threads active. Second, the mail server probably doesn't like it that
you make such a large number of connections simultaneously. If the mail
server doesn't protest, probably its systems administrator will.
Moreover there is probably no benefit in having 1000 connections open to
the same mail server because long before there will be another
bottleneck such as your network connection or the CPU load of either
your computer or the mail server, unless you are on a very scalable
A better solution will be to use a Thread Pool with a limited number of
simultaneous threads (my guess is that 10-20 or so would be good
And as I said to my students yesterday: You shouldn't program
multithreaded applications unless you have studied this subject
thoroughly. Of course I don't know how this applies to you.
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org
More information about the Python-list