如何使该Rails应用程序成为多线程?

时间:2019-03-06 15:40:50

标签: ruby-on-rails

我想在我的小问题上提出建议。 这是我的class,这是我的controller。 我想使该应用程序成为多线程。 请告诉我,哪条路最好? 控制器的快速破解无法解决问题:

 if params[:select].present?
            threads = []
        params[:select].each do |item|
        threads << Thread.new {
           tweet = current_user.tweet.detect {
            |t| item == t.name
        } 

           config = {
.....
etc

}
end
threads.each(&:join)

它不起作用,该过程立即停止:

Started GET "/tweets?select%5B%5D=adamasmit&select_action=follow&tag=&tag1=" for 127.0.0.1 at 2019-03-06 01:37:28 +0300
Processing by TweetsController#index as */*
  Parameters: {"select"=>["adamasmit"], "select_action"=>"follow", "tag"=>"", "tag1"=>""}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 11], ["LIMIT", 1]]
  Tweet Load (0.6ms)  SELECT "tweets".* FROM "tweets" WHERE "tweets"."user_id" = ?  [["user_id", 11]]

仅此而已。

这是应用程序正常运行的示例:

  Started GET "/tweets?select=adamasmit&select_action=unfollow&tag=&tag1=" for 127.0.0.1 at 2019-03-06 18:17:43 +0300
Processing by TweetsController#index as */*
  Parameters: {"select"=>"adamasmit", "select_action"=>"unfollow", "tag"=>"", "tag1"=>""}
  User Load (1.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 11], ["LIMIT", 1]]
  Tweet Load (0.3ms)  SELECT "tweets".* FROM "tweets" WHERE "tweets"."user_id" = ?  [["user_id", 11]]
adding follower to an array: cmirnow
adding follower to an array: travel_slovenia
adding follower to an array: godraksha
.....
etc
adding follower to an array: Pkakooza
adding follower to an array: chrissycrew3
adding friend to an array: cmirnow
adding friend to an array: travel_slovenia
adding friend to an array: godraksha
.....
etc
adding friend to an array: Pkakooza
adding friend to an array: chrissycrew3
adding friend to an array: RivaresF
follow: RivaresF
  Rendering tweets/index.html.erb within layouts/application
  Rendered tweets/index.html.erb within layouts/application (3.7ms)
Completed 200 OK in 3311ms (Views: 132.1ms | ActiveRecord: 2.6ms)


   (0.1ms)  begin transaction
   (0.1ms)  commit transaction

您有什么建议?

1 个答案:

答案 0 :(得分:2)

rails方法是使用ActiveJob在后台执行此类昂贵的操作:https://guides.rubyonrails.org/active_job_basics.html