page contents are not refreshed

Discussion in 'Python' started by Gleb Rybkin, Sep 13, 2006.

  1. Gleb Rybkin

    Gleb Rybkin Guest

    when running apache, mod_python in windows.

    This looks pretty strange. Creating a simple python file that shows
    current time will correctly display the time in apache the first time,
    but freezes afterwards and shows the same time on all subsequent clicks
    as long as the file is not modified.

    Any ideas what's wrong? Thanks.

    from mod_python import apache
    from time import strftime, gmtime

    curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    def handler(req):
    req.content_type = "text/plain"
    req.send_http_header()
    req.write(str(curtime))
    return apache.OK
     
    Gleb Rybkin, Sep 13, 2006
    #1
    1. Advertising

  2. Gleb Rybkin

    Steve Holden Guest

    Gleb Rybkin wrote:
    > when running apache, mod_python in windows.
    >
    > This looks pretty strange. Creating a simple python file that shows
    > current time will correctly display the time in apache the first time,
    > but freezes afterwards and shows the same time on all subsequent clicks
    > as long as the file is not modified.
    >
    > Any ideas what's wrong? Thanks.
    >
    > from mod_python import apache
    > from time import strftime, gmtime
    >
    > curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    > def handler(req):
    > req.content_type = "text/plain"
    > req.send_http_header()
    > req.write(str(curtime))
    > return apache.OK
    >

    Try moving the assignment to curtime inside the handler function so it
    isn't just executed once when the module is imported ...

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Sep 13, 2006
    #2
    1. Advertising

  3. Gleb Rybkin

    waylan Guest

    Gleb Rybkin wrote:
    > when running apache, mod_python in windows.
    >
    > This looks pretty strange. Creating a simple python file that shows
    > current time will correctly display the time in apache the first time,
    > but freezes afterwards and shows the same time on all subsequent clicks
    > as long as the file is not modified.
    >
    > Any ideas what's wrong? Thanks.


    The first time the page was requested mod_python compiled and loaded
    your code. Every request after that mod_python refers to the already
    loaded code in memory in which your expression had already been
    evaluated the first time.

    Therefore, you need to make curtime a 'callable object' so that it will
    be re-evaluated on each request. Unfortunelty, I don't recall if simply
    wraping your strftime() expression in a function will be enough or if
    its more complex that that. That said, I **think** this should work:
    >
    > from mod_python import apache
    > from time import strftime, gmtime
    >

    def curtime():
    return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())

    > def handler(req):
    > req.content_type = "text/plain"
    > req.send_http_header()
    > req.write(str(curtime()))
    > return apache.OK
     
    waylan, Sep 13, 2006
    #3
  4. Gleb Rybkin

    Steve Holden Guest

    waylan wrote:
    > Gleb Rybkin wrote:
    >
    >>when running apache, mod_python in windows.
    >>
    >>This looks pretty strange. Creating a simple python file that shows
    >>current time will correctly display the time in apache the first time,
    >>but freezes afterwards and shows the same time on all subsequent clicks
    >>as long as the file is not modified.
    >>
    >>Any ideas what's wrong? Thanks.

    >
    >
    > The first time the page was requested mod_python compiled and loaded
    > your code. Every request after that mod_python refers to the already
    > loaded code in memory in which your expression had already been
    > evaluated the first time.
    >
    > Therefore, you need to make curtime a 'callable object' so that it will
    > be re-evaluated on each request. Unfortunelty, I don't recall if simply
    > wraping your strftime() expression in a function will be enough or if
    > its more complex that that. That said, I **think** this should work:
    >
    >>from mod_python import apache
    >>from time import strftime, gmtime
    >>

    >
    > def curtime():
    > return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    >
    >
    >>def handler(req):
    >> req.content_type = "text/plain"
    >> req.send_http_header()
    >> req.write(str(curtime()))
    >> return apache.OK

    >
    >

    This is a very long way round for a shortcut (though it does have the
    merit of working). Why not just

    def handler(req):
    req.content_type = "text/plain"
    req.send_http_header()
    curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    req.write(str(curtime))
    return apache.OK

    Or even

    def handler(req):
    req.content_type = "text/plain"
    req.send_http_header()
    req.write(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
    return apache.OK

    regards
    Steve
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://holdenweb.blogspot.com
    Recent Ramblings http://del.icio.us/steve.holden
     
    Steve Holden, Sep 13, 2006
    #4
  5. Gleb Rybkin

    tobiah Guest

    Browser Cache?

    Gleb Rybkin wrote:
    > when running apache, mod_python in windows.
    >
    > This looks pretty strange. Creating a simple python file that shows
    > current time will correctly display the time in apache the first time,
    > but freezes afterwards and shows the same time on all subsequent clicks
    > as long as the file is not modified.
    >
    > Any ideas what's wrong? Thanks.
    >
    > from mod_python import apache
    > from time import strftime, gmtime
    >
    > curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    > def handler(req):
    > req.content_type = "text/plain"
    > req.send_http_header()
    > req.write(str(curtime))
    > return apache.OK
    >


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    tobiah, Sep 13, 2006
    #5
  6. Gleb Rybkin

    waylan Guest

    Steve Holden wrote:
    > waylan wrote:

    [snip]
    > >
    > >>from mod_python import apache
    > >>from time import strftime, gmtime
    > >>

    > >
    > > def curtime():
    > > return strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    > >
    > >
    > >>def handler(req):
    > >> req.content_type = "text/plain"
    > >> req.send_http_header()
    > >> req.write(str(curtime()))
    > >> return apache.OK

    > >
    > >

    > This is a very long way round for a shortcut (though it does have the
    > merit of working). Why not just
    >
    > def handler(req):
    > req.content_type = "text/plain"
    > req.send_http_header()
    > curtime = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    > req.write(str(curtime))
    > return apache.OK
    >
    > Or even
    >
    > def handler(req):
    > req.content_type = "text/plain"
    > req.send_http_header()
    > req.write(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()))
    > return apache.OK
    >


    While Steve's examples certainly do the trick in this limited case, I
    assumed that the original poster was just starting with mod_python and
    I was simply trying to explain the bigger picture for future reference.
    As one develops more sophisticated code, simply adding it to the
    `handler` function becomes less desirable. Reacognizing that anything
    that must be reevaluated on each request must be callable will be a
    bigger help IMHO.

    Steve's examples work because the current time is evaluated within
    `handler` and :

    >>> callable(handler)

    True

    While in the the original example:

    >>> callable(curtime)

    False

    Yet in my example:

    >>> callable(curtime)

    True

    Finally, by way of explaination:

    >>> callable.__doc__

    'callable(object) -> bool\n\nReturn whether the object is callable
    (i.e., some kind of function).\nNote that classes are callable, as are
    instances with a __call__() method.'
     
    waylan, Sep 13, 2006
    #6
  7. waylan a écrit :
    (snip)
    >
    > While Steve's examples certainly do the trick in this limited case, I
    > assumed that the original poster was just starting with mod_python and
    > I was simply trying to explain the bigger picture for future reference.
    > As one develops more sophisticated code, simply adding it to the
    > `handler` function becomes less desirable.


    Indeed, but...

    > Reacognizing that anything
    > that must be reevaluated on each request must be callable


    There it gets plain wrong.

    (snip)

    > Steve's examples work because the current time is evaluated within
    > `handler` and :
    >
    >
    >>>>callable(handler)

    >
    > True
    >
    > While in the the original example:
    >
    >
    >>>>callable(curtime)

    >
    > False


    The fact that the object bound to curtime being callable or not doesn't
    change anything to the problem. You can write as many functions as you
    want outside the handler, if they don't get called by the handler, this
    won't be of no use.
     
    Bruno Desthuilliers, Sep 15, 2006
    #7
    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. Mr. x
    Replies:
    3
    Views:
    10,933
    Daisy
    Sep 3, 2003
  2. Alfons Puig
    Replies:
    0
    Views:
    316
    Alfons Puig
    Aug 28, 2003
  3. abdul bari
    Replies:
    1
    Views:
    482
    Stanley
    Aug 29, 2003
  4. Bill Cohagan
    Replies:
    4
    Views:
    4,205
    Jacob Yang [MSFT]
    Dec 5, 2003
  5. sanjay patil via .NET 247

    Previous(Unwanted) event handled when page refreshed by key F5

    sanjay patil via .NET 247, Apr 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    417
    sanjay patil via .NET 247
    Apr 27, 2004
Loading...

Share This Page