473,721 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2339
In article <ma************ *************** ************@py thon.org>,
Gordon Messmer <gm******@u.was hington.eduwrot e:
>
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**@pythoncra ft.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************ *************** ************@py thon.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.setdefau lttimeout() would work just as well.
--
Aahz (aa**@pythoncra ft.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.setdefau lttimeout() 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
setdefaulttimeo ut(), 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************ *************** ************@py thon.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.setdefa ulttimeout() 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
setdefaulttime out(), 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**@pythoncra ft.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
1873
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 python-milter?) P.S. And where can I find information on particular piece of standard library if it is thread-safe or need locking? I recall 'random' module is (was?)
5
2573
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 because in the file pytz/tzinfo.py, I see global variables _timedelta_cache, _datetime_cache, _ttinfo_cache, which are dictionnaries and are used as cache. I always thought that you must protect shared data with locks when multithreading, but I don't...
8
1973
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 is safe to do things like this? Is there a possible concurrent access that could arise?
1
1635
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
9230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9148
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6678
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5994
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.