Jonathan R. wrote:
But it is do-able, although not easy, I don’t think you can get away
with just trying to access the session array itself in a thread that’s
out of the request loop.
I’d add it’s do-able ONLY if you are using a server-side session store.
NOT if you are using the cookie store that’s the new Rails default. With
the cookie store there’s simply no possible way to write to the session
outside the request loop. So if you want to write to a session in a
thread, you’d have to wait() on the thread before returning from your
action, which might defeat the purpose you were trying to use the thread
for in the first place.
Also, if you are planning on using ActiveRecord in this thread, you’ve
got to make sure to do a few other things to ensure your AR calls are
thread-safe. Things which change slightly in different Rails versions,
as Rails community has changed it’s consensus on what sorts of threading
are ‘supported’ in what ways.
And ALSO, even aside from AR, I learned the hard way that (at least if
you’re using the standard 1.8 MRI ruby interpreter), if you want to send
a thread off and let it keep going outside the request-loop, you’d
really better set it’s priority to -1, or it’s going to end up
interfering with your response being returned even though you don’t
think it should.
Contrary to popular belief, it IS possible to do concurrent programming
like this in Rails, in all versions. But it’s a big pain in the neck.
Don’t do it unless you really have to (which I think I do in my
situation), try to find another solution if possible.