Rails threading - several tasks

I am trying to run several tasks, each task is accessing a database, and I am trying to run tasks in separate runs.

I played, tried allow_concurrency, which I set to true, or config.thread_safe! but I get un-defintistic errors, for example, sometimes a class is missing, or a constant ...

here is some code

grabbers = get_grabber_name_list
threads = []
grabbers.each { |grabber|
  threads << Thread.new {
    ARGV[0] = grabber
    if (@@last_run_timestamp[grabber.to_sym].blank? || (@@last_run_timestamp[grabber.to_sym] >= AbstractGrabber.aff_net_accounts(grabber, "grab_interval").seconds.ago))
    Rake::Task["aff_net:import:" + grabber].execute
    @@last_run_timestamp.merge!({grabber.to_sym => Time.now})
  end
  }
}
threads.each {|t| t.join }

thanks

+3
source share
2 answers

I recently applied a Rails application that uses threads and made several discoveries:

-, - (.. ) , . , . , hash/array , , , , , , .

-, ActiveRecord, , . , . , Rails > 2.2, , , . ActiveRecord , , .

, :

mutex = Mutex.new
my_array = []
threads = []
1.upto(10) do |i|
  threads << Thread.new {
     begin
       do_some_stuff
       mutex.synchronize {
         # You'd think that each thread would only touch its own personal
         # array element but without a mutex, I run into problems.
         my_array[i] = some_computed_value
       }
     ensure
       ActiveRecord::Base.connection_pool.release_connection
     end
   }
}
threads.each {|t| t.join}

, , JRuby. , JRuby - , . , - , .

+3

, , . , delayed_job (http://github.com/tobi/delayed_job).

, , .

0

Source: https://habr.com/ru/post/1721873/


All Articles