cgi "print statement" in multithreaded enviroment?

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

  1. vegetax

    vegetax Guest

    How can i use cgi'like print statement in a multitreaded web framework?
    each thread has its own Servlet instance with request/response objects,
    sys.stdout = self.response(which is a file like object) wont work because
    all threads will set the same file object and it will be a concurrence
    mess.

    I am out of ideas here,so any hacks welcome =)
     
    vegetax, May 3, 2005
    #1
    1. Advertising

  2. vegetax wrote:
    > How can i use cgi'like print statement in a multitreaded web framework?
    > each thread has its own Servlet instance with request/response objects,
    > sys.stdout = self.response(which is a file like object) wont work because
    > all threads will set the same file object and it will be a concurrence
    > mess.
    >
    > I am out of ideas here,so any hacks welcome =)
    >


    instead of:

    print "<html>.....</html>

    do:

    print >>self.response, "<html>....</html>"


    --Irmen
     
    Irmen de Jong, May 3, 2005
    #2
    1. Advertising

  3. vegetax

    vegetax Guest

    Irmen de Jong wrote:

    > vegetax wrote:
    >> How can i use cgi'like print statement in a multitreaded web framework?
    >> each thread has its own Servlet instance with request/response objects,
    >> sys.stdout = self.response(which is a file like object) wont work because
    >> all threads will set the same file object and it will be a concurrence
    >> mess.
    >>
    >> I am out of ideas here,so any hacks welcome =)
    >>

    >
    > instead of:
    >
    > print "<html>.....</html>
    >
    > do:
    >
    > print >>self.response, "<html>....</html>"
    >
    >
    > --Irmen


    But i want to use "print" as a commodity feature, print >>
    self.response,'html..' is longer to type than
    self.response.write('html..')

    The only clear way i am thinking right now is to write a preprocessor ,like
    quixote ptl,but thats sloww
     
    vegetax, May 3, 2005
    #3
  4. vegetax

    Paul Rubin Guest

    vegetax <> writes:
    > But i want to use "print" as a commodity feature, print >>
    > self.response,'html..' is longer to type than
    > self.response.write('html..')


    w = self.response.write

    w('html...')
    w('more html...')
    w('still more html')
     
    Paul Rubin, May 3, 2005
    #4
  5. On Mon, 02 May 2005 20:24:02 -0400, vegetax <> wrote:

    >Irmen de Jong wrote:
    >
    >> vegetax wrote:
    >>> How can i use cgi'like print statement in a multitreaded web framework?
    >>> each thread has its own Servlet instance with request/response objects,
    >>> sys.stdout = self.response(which is a file like object) wont work because
    >>> all threads will set the same file object and it will be a concurrence
    >>> mess.
    >>>
    >>> I am out of ideas here,so any hacks welcome =)
    >>>

    >>
    >> instead of:
    >>
    >> print "<html>.....</html>
    >>
    >> do:
    >>
    >> print >>self.response, "<html>....</html>"
    >>
    >>
    >> --Irmen

    >
    >But i want to use "print" as a commodity feature, print >>
    >self.response,'html..' is longer to type than
    >self.response.write('html..')
    >
    >The only clear way i am thinking right now is to write a preprocessor ,like
    >quixote ptl,but thats sloww
    >

    Maybe assign an object to sys.stdout that has write etc methods that check
    what thread is calling and use self.response things that you assign to
    a property of that sys.stdout object instead of directly to sys.stdout.

    I.e., something like

    sys.stdout = my_stdout_dispatcher
    ...
    # property out_channels stores self.response with thread id:
    sys.stdout.out_channels = self.response(which is a file like object)
    ...

    # print => sys.stdout.write => my_stdout_dispatcher.write('from some thread')
    # which atomically looks up right self.response
    print 'from some thread'

    You may need some lock stuff to do this properly
    (to make self.response lookup info access effectively atomic),
    but that's a general idea. I don't know what other thread interaction issues
    you may have with the state of possibly mutable data being printed.

    This is just an idea for an approach. I may not be understanding your problem at all ;-)

    Regards,
    Bengt Richter
     
    Bengt Richter, May 3, 2005
    #5
  6. vegetax

    vegetax Guest

    Bengt Richter wrote:

    > On Mon, 02 May 2005 20:24:02 -0400, vegetax <> wrote:
    >
    >>Irmen de Jong wrote:
    >>
    >>> vegetax wrote:
    >>>> How can i use cgi'like print statement in a multitreaded web framework?
    >>>> each thread has its own Servlet instance with request/response objects,
    >>>> sys.stdout = self.response(which is a file like object) wont work
    >>>> because all threads will set the same file object and it will be a
    >>>> concurrence mess.
    >>>>
    >>>> I am out of ideas here,so any hacks welcome =)
    >>>>
    >>>
    >>> instead of:
    >>>
    >>> print "<html>.....</html>
    >>>
    >>> do:
    >>>
    >>> print >>self.response, "<html>....</html>"
    >>>
    >>>
    >>> --Irmen

    >>
    >>But i want to use "print" as a commodity feature, print >>
    >>self.response,'html..' is longer to type than
    >>self.response.write('html..')
    >>
    >>The only clear way i am thinking right now is to write a preprocessor
    >>,like quixote ptl,but thats sloww
    >>

    > Maybe assign an object to sys.stdout that has write etc methods that check
    > what thread is calling and use self.response things that you assign to
    > a property of that sys.stdout object instead of directly to sys.stdout.
    >
    > I.e., something like
    >
    > sys.stdout = my_stdout_dispatcher
    > ...
    > # property out_channels stores self.response with thread id:
    > sys.stdout.out_channels = self.response(which is a file like object)
    > ...
    >
    > # print => sys.stdout.write => my_stdout_dispatcher.write('from some
    > # thread') which atomically looks up right self.response
    > print 'from some thread'
    >
    > You may need some lock stuff to do this properly
    > (to make self.response lookup info access effectively atomic),
    > but that's a general idea. I don't know what other thread interaction
    > issues you may have with the state of possibly mutable data being printed.
    >
    > This is just an idea for an approach. I may not be understanding your
    > problem at all ;-)
    >
    > Regards,
    > Bengt Richter


    Something like this:

    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()

    Should be a lock for self.files and i am not sure how to make
    thread.get_ident() atomic
     
    vegetax, May 3, 2005
    #6
    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:
    1
    Views:
    397
    Scott David Daniels
    May 3, 2005
  2. Ronen Yacov
    Replies:
    7
    Views:
    344
  3. Ronen Yacov
    Replies:
    3
    Views:
    364
  4. George
    Replies:
    9
    Views:
    355
    George
    Oct 27, 2008
  5. Replies:
    0
    Views:
    288
Loading...

Share This Page