Hello all. I am planning on using ActiveSupport::Notifications and have a question about threading. For the purposes of this question I am assuming config.threadsafe! is enabled. Will a subscriber execute in the same thread as the code that caused it to be triggered or do they run in separate threads? The reason I ask is that I would like to do something like In Controller Code: def action Module.events[Thread.current] = Event.new # Module.events returns a hash # rest of action code end And elsewhere: ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload| event = Module.events[Thread.current] # returns the Event that was created in the controller. # Do processing on event end Obviously if the Subscriber runs in a different thread then this will not work since Thread.current will not match so I wanted to check. Note that in this case I do not want to pass the Event as part of the payload because I want to be able to use existing rails built-in notifications without having to modify them. Cheers Jeff -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1V_DB1nyqkMJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Sep-12 12:00 UTC
Re: ActiveSupport::Notifications threading question
On Wednesday, September 12, 2012 2:11:41 AM UTC+1, Jeffrey Jones wrote:> > Hello all. > > I am planning on using ActiveSupport::Notifications and have a question > about threading. For the purposes of this question I am assuming > config.threadsafe! is enabled. > > Will a subscriber execute in the same thread as the code that caused it to > be triggered or do they run in separate threads? > > I would have thought that was easy to test. Currently everything happenson the thread triggering the event. There''s also code in rails (eg ActiveRecord''s logsubscriber) that use thread local variables in notification processing so I wouldn''t that would be changed in a hurry. Fred> The reason I ask is that I would like to do something like > > In Controller Code: > > def action > Module.events[Thread.current] = Event.new # Module.events returns a hash > # rest of action code > end > > And elsewhere: > > ActiveSupport::Notifications.subscribe "process_action.action_controller" > do |name, start, finish, id, payload| > event = Module.events[Thread.current] # returns the Event that was > created in the controller. > # Do processing on event > end > > Obviously if the Subscriber runs in a different thread then this will not > work since Thread.current will not match so I wanted to check. Note that in > this case I do not want to pass the Event as part of the payload because I > want to be able to use existing rails built-in notifications without having > to modify them. > > Cheers > > Jeff > > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zaXAeHVURIUJ. For more options, visit https://groups.google.com/groups/opt_out.
Jeffrey Jones
2012-Sep-13 00:06 UTC
Re: Re: ActiveSupport::Notifications threading question
I have done various testing using MRI and JRuby with config.threadsafe! enabled and I THINK that it does always execute in the same thread. However since the log files are interleaved when dealing with many requests and logs written from the subscriber do not have access to the request object for logging with a tag using something like uuid I cannot be 100% sure which is why I was hoping someone would be able to give a definitive answer. cheers Jeff On 12/09/12 21:00, Frederick Cheung wrote:> > > On Wednesday, September 12, 2012 2:11:41 AM UTC+1, Jeffrey Jones wrote: > > Hello all. > > I am planning on using ActiveSupport::Notifications and have a > question about threading. For the purposes of this question I am > assuming config.threadsafe! is enabled. > > Will a subscriber execute in the same thread as the code that > caused it to be triggered or do they run in separate threads? > > I would have thought that was easy to test. Currently everything > happens on the thread triggering the event. There''s also code in rails > (eg ActiveRecord''s logsubscriber) that use thread local variables in > notification processing so I wouldn''t that would be changed in a hurry. > > Fred > > The reason I ask is that I would like to do something like > > In Controller Code: > > def action > Module.events[Thread.current] = Event.new # Module.events > returns a hash > # rest of action code > end > > And elsewhere: > > ActiveSupport::Notifications.subscribe > "process_action.action_controller" do |name, start, finish, id, > payload| > event = Module.events[Thread.current] # returns the Event that > was created in the controller. > # Do processing on event > end > > Obviously if the Subscriber runs in a different thread then this > will not work since Thread.current will not match so I wanted to > check. Note that in this case I do not want to pass the Event as > part of the payload because I want to be able to use existing > rails built-in notifications without having to modify them. > > Cheers > > Jeff > > > > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/zaXAeHVURIUJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jeffrey Jones
2012-Sep-13 00:27 UTC
Re: Re: ActiveSupport::Notifications threading question
Sorry I was talking out of my arse in the last post. Correct version: I have done various testing using MRI and JRuby with config.threadsafe! and it appears to work in all the tests I have thrown at it, but that gives me 99% certainty (I could have missed soemthing). I tried tracing through the code on github but quickly got lost which is why I asked the question here to see if someone (who does know the code) could give a definitive "Yes" or "no" Cheers Jeff On 13/09/12 09:06, Jeffrey Jones wrote:> I have done various testing using MRI and JRuby with > config.threadsafe! enabled and I THINK that it does always execute in > the same thread. > > However since the log files are interleaved when dealing with many > requests and logs written from the subscriber do not have access to > the request object for logging with a tag using something like uuid I > cannot be 100% sure which is why I was hoping someone would be able to > give a definitive answer. > > cheers > > Jeff > > On 12/09/12 21:00, Frederick Cheung wrote: >> >> >> On Wednesday, September 12, 2012 2:11:41 AM UTC+1, Jeffrey Jones wrote: >> >> Hello all. >> >> I am planning on using ActiveSupport::Notifications and have a >> question about threading. For the purposes of this question I am >> assuming config.threadsafe! is enabled. >> >> Will a subscriber execute in the same thread as the code that >> caused it to be triggered or do they run in separate threads? >> >> I would have thought that was easy to test. Currently everything >> happens on the thread triggering the event. There''s also code in >> rails (eg ActiveRecord''s logsubscriber) that use thread local >> variables in notification processing so I wouldn''t that would be >> changed in a hurry. >> >> Fred >> >> The reason I ask is that I would like to do something like >> >> In Controller Code: >> >> def action >> Module.events[Thread.current] = Event.new # Module.events >> returns a hash >> # rest of action code >> end >> >> And elsewhere: >> >> ActiveSupport::Notifications.subscribe >> "process_action.action_controller" do |name, start, finish, id, >> payload| >> event = Module.events[Thread.current] # returns the Event that >> was created in the controller. >> # Do processing on event >> end >> >> Obviously if the Subscriber runs in a different thread then this >> will not work since Thread.current will not match so I wanted to >> check. Note that in this case I do not want to pass the Event as >> part of the payload because I want to be able to use existing >> rails built-in notifications without having to modify them. >> >> Cheers >> >> Jeff >> >> >> >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-talk/-/zaXAeHVURIUJ. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> > > -- > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Sep-13 13:54 UTC
Re: Re: ActiveSupport::Notifications threading question
On Thursday, September 13, 2012 1:28:11 AM UTC+1, Jeffrey Jones wrote:> > Sorry I was talking out of my arse in the last post. Correct version: > > I have done various testing using MRI and JRuby with config.threadsafe! > and it appears to work in all the tests I have thrown at it, but that gives > me 99% certainty (I could have missed soemthing). > > I tried tracing through the code on github but quickly got lost which is > why I asked the question here to see if someone (who does know the code) > could give a definitive "Yes" or "no" > >And of course there''s the difference between something just happening to behave in a certain way with the current implementation and something which a documented part of the interface. There''s not much in the way of documentation around this stuff but given that existing rails internals assume that the delivery thread is the same as the thread processing the notification I''d say it was pretty safe to rely on. Fred> Cheers > > Jeff > > On 13/09/12 09:06, Jeffrey Jones wrote: > > I have done various testing using MRI and JRuby with config.threadsafe! > enabled and I THINK that it does always execute in the same thread. > > However since the log files are interleaved when dealing with many > requests and logs written from the subscriber do not have access to the > request object for logging with a tag using something like uuid I cannot be > 100% sure which is why I was hoping someone would be able to give a > definitive answer. > >> cheers > > Jeff > > On 12/09/12 21:00, Frederick Cheung wrote: > > > > On Wednesday, September 12, 2012 2:11:41 AM UTC+1, Jeffrey Jones wrote: >> >> Hello all. >> >> I am planning on using ActiveSupport::Notifications and have a question >> about threading. For the purposes of this question I am assuming >> config.threadsafe! is enabled. >> >> Will a subscriber execute in the same thread as the code that caused it >> to be triggered or do they run in separate threads? >> >> I would have thought that was easy to test. Currently everything > happens on the thread triggering the event. There''s also code in rails (eg > ActiveRecord''s logsubscriber) that use thread local variables in > notification processing so I wouldn''t that would be changed in a hurry. > > Fred > > > >> The reason I ask is that I would like to do something like >> >> In Controller Code: >> >> def action >> Module.events[Thread.current] = Event.new # Module.events returns a hash >> # rest of action code >> end >> >> And elsewhere: >> >> ActiveSupport::Notifications.subscribe "process_action.action_controller" >> do |name, start, finish, id, payload| >> event = Module.events[Thread.current] # returns the Event that was >> created in the controller. >> # Do processing on event >> end >> >> Obviously if the Subscriber runs in a different thread then this will not >> work since Thread.current will not match so I wanted to check. Note that in >> this case I do not want to pass the Event as part of the payload because I >> want to be able to use existing rails built-in notifications without having >> to modify them. >> >> Cheers >> >> Jeff >> >> >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> > . > To unsubscribe from this group, send email to > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/zaXAeHVURIUJ. > For more options, visit https://groups.google.com/groups/opt_out. > > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonra...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:> > . > To unsubscribe from this group, send email to > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > For more options, visit https://groups.google.com/groups/opt_out. > > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/z-d2jeb0Cf4J. For more options, visit https://groups.google.com/groups/opt_out.