Simple mod_ruby counter using global variable

R

Randy Lawrence

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}"
 
S

Sean O'Dell

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
 
L

Lennon Day-Reynolds

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
 
D

David Morton

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

Or at least look at how it is handled there.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top