Access `session` within rails controller thread

前端 未结 2 1855
甜味超标
甜味超标 2021-01-28 04:35

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] = \         


        
2条回答
  •  耶瑟儿~
    2021-01-28 05:25

    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?

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

提交回复
热议问题