Re: session variables and threading.

Giulio Mola
Sat, 22 Oct 2005 17:43:30 -0000

Hi again,

I doubth that someone has ever found this interesting ... anyway, I
think that the problem is that the session variable is stored only
once, when the controller terminate its execution.

In your case the session variable is modified in a thread, after that
the controller has terminated, and thus the session is never stored.
To overcome this you can call
@session.update within the thread code, to store the content of the
variable and make it available to other methods/controllers.

something like this should work:

 def startstatus
         @t = Thread.new {
                 10.times do |i|
                     @session[''status''] = rand(0).to_s
                     @session.update
                     sleep 2;
                 end
         }
 end


Cheers
/Giulio



On 10/21/05, Giulio Mola <lamola-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> Hi Gnan,
>
> I am facing the same problem in one application I am trying to
> develop, where I must use threads to run external processes, and I
> want to use periodically_call_remote to display some status info to
> the user.
>
> This is similar to what you''re trying to do, so I have tested your
> code, adding some debug text.
> And this is what I got:
>
> dbg:(startstatus) thread has updated status value to 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(startstatus) thread has updated status value to 0.791096143890172
> dbg:(startstatus) thread has updated status value to 0.608514182502404
> dbg:(status) status is 0.424670516280457
> dbg:(startstatus) thread has updated status value to 0.47138453903608
> dbg:(startstatus) thread has updated status value to 0.421978918369859
> dbg:(status) status is 0.424670516280457
> dbg:(startstatus) thread has updated status value to 0.851542412070557
> dbg:(startstatus) thread has updated status value to 0.970735334325582
> dbg:(status) status is 0.424670516280457
> dbg:(startstatus) thread has updated status value to 0.438931414391845
> dbg:(startstatus) thread has updated status value to 0.976107021328062
> dbg:(status) status is 0.424670516280457
> dbg:(startstatus) thread has updated status value to 0.291818090947345
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
> dbg:(status) status is 0.424670516280457
>
> So the thread is updating the session variable, but these updates go
> lost somewhere...
> Is this a Rails problem?
> Does someone want to take the time to look into this?
>
> Cheers
> Giulio
>
>
> On 9/10/05, Gnanavel
<gnanavel.s-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > Hi All,
> >
> > For my problem of updating status in the view, i tried the following :
> >
> > Controller :
> > def status
> >         #@session[''status''] = rand(0).to_s
> >         @status = @session[''status'']
> >         render :partial => "status", :locals => {
:status => @status }
> > end
> >
> > def startstatus
> >         @t = Thread.new {
> >                 10.times do |i|
> >                     @session[''status''] =
rand(0).to_s
> >                     sleep 2;
> >                 end
> >         }
> > end
> >
> > My startstatus.rhtml looks like this
> >
> > <%= javascript_include_tag "prototype" %>
> >
> >     <%= render_component :action => "status" %>
> >     <%= periodically_call_remote(:url => { :action => :status
},
> >                                  :frequency => 4,
> >                                  :update =>
''update1'' ) %>
> >
> >
> > _status.rhtml (render via partial)
> >
> > <%= @status %>...
> >
> >
> >
> > However the value of the @session[''status''] does not
chnage in the view,
> > looks like the thread has been killed or something? I only get the
firts
> > value and then on the value does not chnage every 4 secs.
> >
> > Let me know what should I have to change for getting the value of the
> > session from t§he thread.
> >
> > Regards
> > Gnan
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>