Simple mod_ruby counter using global variable

Discussion in 'Ruby' started by Randy Lawrence, Jul 2, 2004.

  1. I'm guessing this is because each Apache process has its own Ruby
    interpreter instance:

    The following code inside a mod_ruby script does not always increment
    $total displayed in the browser (it actually increments $total
    internally, but each time the page is refreshed, the next Apache process
    is used in seemingly round-robin fashion):

    -----
    if defined?($total)
    $total = $total + 1
    else
    $total = 0
    end
    puts "total = #{$total}"
    -----

    Does anyone know the simplest way to deal with this other than using
    persistent storage like database or file?
    Randy Lawrence, Jul 2, 2004
    #1
    1. Advertising

  2. Randy Lawrence

    Sean O'Dell Guest

    On Friday 02 July 2004 13:29, Randy Lawrence wrote:
    > I'm guessing this is because each Apache process has its own Ruby
    > interpreter instance:
    >
    > The following code inside a mod_ruby script does not always increment
    > $total displayed in the browser (it actually increments $total
    > internally, but each time the page is refreshed, the next Apache process
    > is used in seemingly round-robin fashion):
    >
    > -----
    > if defined?($total)
    > $total = $total + 1
    > else
    > $total = 0
    > end
    > puts "total = #{$total}"


    You could run your application as a stand-alone server and have your CGI call
    it, but that would be a little more involved than simply tracking the total
    through a file or database.

    Sean O'Dell
    Sean O'Dell, Jul 2, 2004
    #2
    1. Advertising

  3. A file would work, but you'd have to be careful to lock it before each
    write, or you could lose updates. It might be better to run a
    single-purpose 'counting server' listening on a socket, which
    maintained an internal count variable, and incremented and echoed it
    across the wire every time a client connected. It's more work than
    just setting a global, but no persistence is involved, and you don't
    have to worry about race conditions -- just handle the requests on a
    first-come, first-served basis.

    Lennon
    Lennon Day-Reynolds, Jul 3, 2004
    #3
  4. Randy Lawrence

    David Morton Guest

    I would use the Application or Session objects in the Ruby::ASP environment.

    Or at least look at how it is handled there.



    Randy Lawrence wrote:
    > I'm guessing this is because each Apache process has its own Ruby
    > interpreter instance:
    >
    > The following code inside a mod_ruby script does not always increment
    > $total displayed in the browser (it actually increments $total
    > internally, but each time the page is refreshed, the next Apache process
    > is used in seemingly round-robin fashion):
    >
    > -----
    > if defined?($total)
    > $total = $total + 1
    > else
    > $total = 0
    > end
    > puts "total = #{$total}"
    > -----
    >
    > Does anyone know the simplest way to deal with this other than using
    > persistent storage like database or file?
    >
    David Morton, Jul 5, 2004
    #4
    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. The Eeediot
    Replies:
    3
    Views:
    2,225
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  2. Bryan Parkoff
    Replies:
    2
    Views:
    6,490
  3. George2
    Replies:
    1
    Views:
    792
    Alf P. Steinbach
    Jan 31, 2008
  4. Sumit Nathany

    Problem in using unsigned variable as a counter

    Sumit Nathany, Oct 24, 2008, in forum: C Programming
    Replies:
    2
    Views:
    517
    Sumit Nathany
    Oct 24, 2008
  5. Robert McKinnon
    Replies:
    1
    Views:
    167
    Dave Thomas
    Mar 6, 2004
Loading...

Share This Page