ruby-on-railsrubycelluloid

Ruby Celluloid "environment"


I am very new to Celluloid, but I don't understand why, in my Rails app, my futures loose the current locale:

puts "locale OUTSIDE #{I18n.locale}"
data.map do |item|
  Celluloid::Future.new { puts "locale INSIDE #{I18n.locale}"; serialize_item(item) }
end.map(&:value)

basically I got something like

locale OUTSIDE en
locale INSIDE it
locale INSIDE it
locale INSIDE it
locale INSIDE it
locale INSIDE itlocale INSIDE it
locale INSIDE it

locale INSIDE it

locale INSIDE it

and when I change locale the futures keep considering my default locale :it

I feel like I am missing something basic...


Solution

  • Celluloid futures get executed in a separate thread pool (not a different process as some answers suggest),

    I18n.locale value gets stored in a variable scoped to the individual thread http://www.rubydoc.info/docs/rails/2.3.8/I18n%2FConfig%3Alocale :

    The only configuration value that is not global and scoped to thread is :locale. It defaults to the default_locale.

    Meaning that the config you set outside of the worker threads does not apply within the worker threads.

    You need to:

    1. use I18n.default_locale if you want the config to be propagated to all worker threads in the process, OR
    2. send the locale as a parameter into the future and set it (be sure to clear it again in an ensure block or funny things might happen with other celluloid actors)