multithreading - Threading in Rails - do params[] persist? -


i trying spawn thread in rails. not comfortable using threads need have in-depth knowledge of rails' request/response cycle, yet cannot avoid using 1 request times out.

in order avoid time out, using thread within request. question here simple. thread i've used accesses params[] variable inside it. , things seem work ok now. want know whether right? i'd happy if can throw light on using threads in rails during request/response cycle.

[starting bounty]

the short answer yes, degree; binding in thread created continue persist. params still exist if no 1 (including rails) goes out of way modify or delete params hash. instead, rely on garbage collector clean these objects. since thread has access current context (called "binding" in ruby) when created, variables can reached scope (effectively entire state when thread created) cannot deleted garbage collector. however, executing continues in main thread, values of variables in context can changed main thread, or thread created, if can access it. benefit--and downfall--of threads: share memory else.

you can emulate similar environment rails test problem, using function such: http://gist.github.com/637719. can see, code still prints 5.

however, not correct way this. better way pass data thread pass thread.new, so:

# dup objects when passing thread, else # haven't done good-it still same memory thread.new(params.dup) |params|   puts params[:foo] end 

this way, can sure modifications params not affect thread. best practice only use data pass thread in way, or things thread created. relying on state of program outside thread dangerous.

so can see, there reasons not recommended. multithreaded programming hard, in ruby, , especially when you're dealing many libraries , dependencies used in rails. in fact, ruby seems make deceptively easy, it's trap. bugs encounter subtle; happen "randomly" , hard debug. why things resque, delayed job, , other background processing libraries used , recommended rails apps, , recommend same.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -