cgi "print statement" in multithreaded enviroment?

Scott David Daniels Scott.Daniels at Acm.Org
Tue May 3 10:35:16 EDT 2005


vegetax wrote:
> So it would be something like this, right??
> 
> class ThreadSpecificFile:
>          def __init__(s):
>              self.files = []
>          def set_stdout(f,thread_id):
>              self.files[thread_id] = f
>          def clean_up(thread_id):
>              del self.files[thread_id]
>          def write(data):
>              self.files[thread.get_ident()].write(data)
> 
> sys.stdout = ThreadSpecificFile()

     import sys, thread

     class ThreadSpecificFile:
         def __init__(self, original):
             self.files = dict()
             self.original = original

         def set_stdout(self, f):
             self.files[thread.get_ident()] = f

         def close(self):
             try:
                 this = self.stops().close()
             except KeyError:
                 pass

         def stops(self):
             return self.files.pop(thread.get_ident())

         def write(self, data):
             self.files.get(thread.get_ident(),
                            self.original).write(data)
             except KeyError:
                 self

     sys.stdout = ThreadSpecificFile(sys.stdout)


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list