在 rails 控制器线程中访问 `session` [英] Access `session` within rails controller thread

查看:44
本文介绍了在 rails 控制器线程中访问 `session`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 rails 的线程中查看/更改会话数据?在我的控制器中查看此代码片段:

How do I see / change session data from within a thread within rails? See this code snippet inside my controller:

def controller_action
  session[:something] = 'before'   # works properly
  Thread.new do
    session[:something] = 'after'  # out of scope.  <--- how do I fix this?
  end
end

(与这个问题相关,但更具体.我期待完全不同的答案.)

(related to this question, but much more specific. I expect quite different answers.)

推荐答案

我仍然不确定为什么 session 不会通过线程内的引用在范围内,但如果这是真的,您可以尝试将当前绑定传递给线程作为参数:

I'm still not sure why session would not be in scope by reference inside a thread, but if this is true, you could try passing the current binding to the thread as an argument:

Thread.new(binding) do 
    thread_session = eval("session", binding)
end

或者类似的东西.我仍然不相信会话是通过值而不是线程中的引用.为了检查这一点,我只是将一些散列传递到控制台上的新线程中,并且对线程内部散列所做的更改在该线程外部可见.是否有一些魔法使会话行为不同?

Or something like that. I'm still not convinced that the session is by value and not by reference in the Thread. To check this I just passed some hashes into new threads on the console, and changes made to a hash inside a thread are visible outside that thread. Is there some magic that makes session behave differently?

DelayedJob 不是处理分拆的长时间运行流程的更好方法吗从用户请求?

Wouldn't DelayedJob be a better way to handle a long-running process spun off from a user request?

这篇关于在 rails 控制器线程中访问 `session`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆