(solved) smtplib, capturing output from set_debuglevel ??

Josiah Carlson jcarlson at uci.edu
Sat Oct 9 13:53:44 EDT 2004


> >In fact, if I were to make one suggestion to Tim, it
> >would be to switch from his custom version of smtplib to the version in
> >CVS, then do anything to smtplib.stderr that he wants.
> 
> Thanks,   I will give that a go now :-)
> 
> Will each thread's smtplib.stderr be isolated from instances(?)   in other
> threads?

The class I gave you earlier, you can make the following assignment
/once/:
  smtplib.stderr = multi_thread_output()

Then you can spawn as many threads as your heart desires, and it will
send each individual thread's output to its own little buffer.

When the thread disappears, its output becomes available via
obj.get_ready()

If you have a 'controlling thread' that can periodically poll this, then
the problem is basically solved.  It would also be reasonable to have each
of your SMTP threads print its output when it is done.  Below is a
variant that works well for that, and a bit of controlling code.

 - Josiah


def smtp_thread():
    #create SMTP instance
    #set debug level
    #connect
    #handle all your smtp commands
    #disconnect
    print smtplib.stderr.get_mine()


class multi_thread_output:
    def __init__(self):
        self.lock = threading.Lock()
        self.ios = {}
    def write(self, data):
        thread = threading.currentThread().getName()
        try:
            self.lock.acquire()
            if thread in self.ios:
                self.ios[thread].write(data)
            else:
                t = self.ios[thread] = StringIO()
                t.write(data)
        finally:
            self.lock.release()
    def get_ready(self):
        names = {}
        for th in threading.enumerate():
            names[th.getName()] = None
        try:
            self.lock.acquire()
            for name in self.ios:
                if name not in names:
                    r = self.ios.pop(name)
                    r.seek(0)
                    return r
        finally:
            self.lock.release()
    def get_mine(self):
        name = threading.currentThread().getName()
        try:
            self.lock.acquire()
            if name in self.ios:
                r = self.ios.pop(name)
                r.seek(0)
                return r
            return ''
        finally:
            self.lock.release()




More information about the Python-list mailing list