Re: cgi "print statement" in multithreaded enviroment?

Discussion in 'Python' started by vegetax, May 3, 2005.

  1. vegetax

    vegetax Guest

    vegetax wrote:

    > Jeff Epler wrote:
    >
    >> You could write something like
    >> class ThreadSpecificFile:
    >> def set_stdout(f):
    >> self.files[thread_id] = f
    >> def write(data):
    >> self.files[thread_id].write(data)
    >> sys.stdout = ThreadSpecificFile()
    >> where you'll have to fill out a few more things like thread_id,
    >> __init__, and a way to clean up items from self.files when a thread
    >> passes away.
    >>
    >> Jeff

    >
    > Thats what i was looking for,Thanks =)
    >


    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()
    vegetax, May 3, 2005
    #1
    1. Advertising

  2. 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 David Daniels, May 3, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vegetax
    Replies:
    5
    Views:
    384
    vegetax
    May 3, 2005
  2. Ronen Yacov
    Replies:
    7
    Views:
    317
  3. Ronen Yacov
    Replies:
    3
    Views:
    345
  4. George
    Replies:
    9
    Views:
    330
    George
    Oct 27, 2008
  5. Replies:
    0
    Views:
    255
Loading...

Share This Page