Is it possible to share data between threads in rails? I started a thread in an action in a controller, and assigned the thread to a class variable (@@thread), but that gets cleared the next time i call an action on that controller... I know the thread is still running, but i don''t know how to pass data to/from it... hmm, after further tests, i''ve realised that since im running under apache, each fcgi process gets its own environment... is that right? so it isn''t possible to share data around like that. I guess it also goes against the "push everything into the database" model of ruby-on-rails right? I guess the best approach is push the data i need into the database, is that what more experienced railers would think of as the-rails-way? any pointers would be great, bodhi
> I guess the best approach is push the data i need into the database, > is that what more experienced railers would think of as the-rails-way?You definitely want to stay away from threads. They won''t work at all under fcgi, mod_ruby, or cgi. Push integration to a shared source that''ll enable you to scale with. Either database, DRb, or memcached. I''d recommend trying the database first. -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://www.loudthinking.com/ -- Broadcasting Brain
On Wed, 16 Feb 2005, Bodhi Philpot wrote:> Is it possible to share data between threads in rails? I started a thread in > an action in a controller, and assigned the thread to a class variable > (@@thread), but that gets cleared the next time i call an action on that > controller... I know the thread is still running, but i don''t know how to > pass data to/from it... > > hmm, after further tests, i''ve realised that since im running under apache, > each fcgi process gets its own environment... is that right? so it isn''t > possible to share data around like that. I guess it also goes against the > "push everything into the database" model of ruby-on-rails right? > > I guess the best approach is push the data i need into the database, is that > what more experienced railers would think of as the-rails-way? > > any pointers would be great, > > bodhiyou should read about fastcgi session affinity. if it''s setup you can know that a certain fastcgi responder (vs any old one out of a pool) will handle a request. then, by setting global (or class) variables you can have access to them between requests. note that this is a fantastic was to create a fastcgi process that grows without bound : your programming must not only clean up after itself, but adds some sort of sweep process to reclaim data from sessions that were abandoned by the client. cheers. -a -- ==============================================================================| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================
On 16/02/2005, at 7:20 PM, David Heinemeier Hansson wrote:>> I guess the best approach is push the data i need into the database, >> is that what more experienced railers would think of as >> the-rails-way? > > You definitely want to stay away from threads. They won''t work at all > under fcgi, mod_ruby, or cgi. Push integration to a shared source > that''ll enable you to scale with. Either database, DRb, or memcached. > I''d recommend trying the database first.Its working here for me under Apache+FCGI... The only problem is (was) communication between threads... i pushed that into the database and my solution became much simpler :) I still need the threads as im doing some heavy offline processing that i dont want the user to have to wait for, but I want them to be able to check the status.