From: Bill Kelly Date: 2003-01-23T15:52:39+09:00 Subject: Re: mod_ruby: what's persistent and what's shared? Hi, From: "Tom Sawyer" > > i'm tired of being confused about this and i'm hoping someone can clear this > up for me. > > when i make a request to apache on a mod_ruby script, i know that the ruby > interpreter is embedded into apache so that a new instace of ruby does not > have to be started every time. so then global variables and class definitions > including class variables persist between connections. right? so if i'm USER1 > and i request a ruby app that creates a global variable or an object instance > with a defined class variable, then when USER2 requests the same app, will > the gloabal variable or class variable be set as it was for USER1? Sadly, from what I understand so far, it's the "worst of both worlds". ;-D You have to be aware of persistence because global variables do persist in the Ruby interpreter running in a given httpd child process. But, you can't depend on persistence, because the multiple (forked) httpd processes that might service your request are all separate. [Each has its "own" persistence.] If you make a test script and keep refreshing the page, you'll tend to get the same process handling the request, if the HTTP connection is in a keep-alive state. So you'll see apparent persistence from invocation to invocation. But once a different process takes over and services the request, you'll see a whole "different" persistence associated with that particular process / Ruby interpreter. Below is a little test script that prints the process number, and also the contents of a hash that, if all processes were able to share the same global interpreter data, would grow to include keys for each process, the value of which would be an integer incremented each time a given process invoked the script. (But of course, what you'll see is that the separate processes cannot share the same hash/interpreter data.) (Also if you have access to http://your-server/server-status you can see a graph in ASCII of the various httpd processes and their current states, which is nice for seeing one still in the keep-alive state...) Hope this helps, Bill ---------------------------------- #!/usr/local/bin/ruby -w require "cgi" $gh ||= Hash.new(0) $gh[$$] += 1 cgi = CGI.new("html3") cgi.out { cgi.html { cgi.body { cgi.h1 { "Howdy from #$$: #{ $gh.inspect } ..." } } } } #end-of-file