473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

thread safe SMTP module

I believe that I've seen this discussed previously, so maybe there's
some interest in it. I wrote a threaded mail filtering framework a
while ago, and one of the modules does address verification via SMTP.
Since smtplib.SMTP uses blocking IO, it can block the whole
interpreter. Sometimes the whole thing would stop working indefinitely.

I'm now aware that Twisted offers a non-blocking SMTP class, but I
didn't really want to make that a dependency of the address
verification. I ended up subclassing the smtplib.SMTP class and
rewriting the functions that do I/O. Perhaps someone who doesn't want
to install Twisted will find this class useful, someday. It doesn't
support TLS, but it is otherwise a thread-safe SMTP class.

Mar 2 '07 #1
5 2320
In article <ma***************************************@python. org>,
Gordon Messmer <gm******@u.washington.eduwrote:
>
I believe that I've seen this discussed previously, so maybe there's
some interest in it. I wrote a threaded mail filtering framework
a while ago, and one of the modules does address verification
via SMTP. Since smtplib.SMTP uses blocking IO, it can block the
whole interpreter. Sometimes the whole thing would stop working
indefinitely.
That doesn't make any sense. Blocking I/O generally releases the GIL,
which is the whole reason Python doesn't totally suck for threading.
There may be other thread problems, but I doubt that you have correctly
analyzed their source. You can prove this for yourself by trying out my
threaded spider at
http://www.pythoncraft.com/OSCON2001...dPoolSpider.py
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Mar 4 '07 #2
Aahz wrote:
>
That doesn't make any sense. Blocking I/O generally releases the GIL,
which is the whole reason Python doesn't totally suck for threading.
Nevertheless, among the caveats listed at
http://docs.python.org/lib/module-thread.html is:

"Not all built-in functions that may block waiting for I/O allow other
threads to run. (The most popular ones (time.sleep(), file.read(),
select.select()) work as expected.)"
There may be other thread problems, but I doubt that you have correctly
analyzed their source.
I subclassed smtplib.SMTP and replaced only the lines of code that had
to do with blocking IO (connect, send and receive operations).
Beforehand, python would occasionally lock up. Having made those
changes, python stopped locking up. I think the problem was pretty well
apparent. I can't pin it down to which one of those three operations
was at fault, and it may be that only one was. However, when I use
non-blocking IO, the application works. When I used smtplib.SMTP, it
didn't.

I'm open to other explanations.
Mar 4 '07 #3
In article <ma***************************************@python. org>,
Gordon Messmer <yi*****@eburg.comwrote:
>Aahz wrote:
>>
That doesn't make any sense. Blocking I/O generally releases the GIL,
which is the whole reason Python doesn't totally suck for threading.

Nevertheless, among the caveats listed at
http://docs.python.org/lib/module-thread.html is:

"Not all built-in functions that may block waiting for I/O allow other
threads to run. (The most popular ones (time.sleep(), file.read(),
select.select()) work as expected.)"
That's why I said "generally".
>There may be other thread problems, but I doubt that you have correctly
analyzed their source.

I subclassed smtplib.SMTP and replaced only the lines of code that had
to do with blocking IO (connect, send and receive operations).
Beforehand, python would occasionally lock up. Having made those
changes, python stopped locking up. I think the problem was pretty well
apparent. I can't pin it down to which one of those three operations
was at fault, and it may be that only one was. However, when I use
non-blocking IO, the application works. When I used smtplib.SMTP, it
didn't.

I'm open to other explanations.
Assuming you have correctly tracked down the problem area, I would call
that a thread bug in Python. But my experience is that you simply have
run into a problem with the socket. I would suggest that using
socket.setdefaulttimeout() would work just as well.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Mar 4 '07 #4
Aahz wrote:
>
Assuming you have correctly tracked down the problem area, I would call
that a thread bug in Python. But my experience is that you simply have
run into a problem with the socket. I would suggest that using
socket.setdefaulttimeout() would work just as well.
I believe that solution, also would not work. This note is included in
the socket documentation, regarding "timeout mode":

http://docs.python.org/lib/socket-objects.html
"A consequence of this is that file objects returned by the makefile()
method must only be used when the socket is in blocking mode; in timeout
or non-blocking mode file operations that cannot be completed
immediately will fail."

smtplib.SMTP uses file objects when reading SMTP responses. If I used
setdefaulttimeout(), then the socket would be in timeout mode and the
above note would be applicable.

I am not at all above calling python's behavior a bug, except that it
seemed like a known behavior given the note in the thread documentation
regarding built-in functions that block on I/O.
Mar 4 '07 #5
In article <ma***************************************@python. org>,
Gordon Messmer <yi*****@eburg.comwrote:
>Aahz wrote:
>>
Assuming you have correctly tracked down the problem area, I would call
that a thread bug in Python. But my experience is that you simply have
run into a problem with the socket. I would suggest that using
socket.setdefaulttimeout() would work just as well.

I believe that solution, also would not work. This note is included in
the socket documentation, regarding "timeout mode":

http://docs.python.org/lib/socket-objects.html
"A consequence of this is that file objects returned by the makefile()
method must only be used when the socket is in blocking mode; in timeout
or non-blocking mode file operations that cannot be completed
immediately will fail."

smtplib.SMTP uses file objects when reading SMTP responses. If I used
setdefaulttimeout(), then the socket would be in timeout mode and the
above note would be applicable.
Hrm. At this point, I would suggest taking discussion to python-dev; it
has been too long since I looked closely at thread/socket behavior.
>I am not at all above calling python's behavior a bug, except that it
seemed like a known behavior given the note in the thread documentation
regarding built-in functions that block on I/O.
No, at this point I think this is neither bug nor about thread blocking
on I/O. I think it's about sockets dying and the inability of sockets in
blocking mode to recover. I have seen this kind of behavior in
single-threaded systems. But it really needs someone who knows more than
I do, and I think the first step here is to go ahead and file a bug
report for tracking purposes.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"I disrespectfully agree." --SJM
Mar 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roman Suzi | last post by:
(this is a repost with an addition - probably noone noticed my message first time) Hi! Just to be sure, is email package of Python 2.3 thread-safe or not (to use, for example, in...
5
by: nicolas_riesch | last post by:
Does someone know if the module pytz (http://sourceforge.net/projects/pytz/) is thread-safe ? I have not seen it explicitely stated, and just wanted to be sure, as I want to use it. That's...
8
by: Philippe Lang | last post by:
Hello, I need to send emails from Postgresql triggers. I was able to do it with a 'plperlu' function, that calls the 'system' perl function, that itself calls the 'mail' shell function. Is...
1
by: =?ISO-8859-1?Q?J=E9r=E9my_Wagner?= | last post by:
Hi, I recently tried to use the subprocess module within a threading.Thread class, but it appears the module is not thread-safe. What is the policy of python regarding thread-safety of a module ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.