2012-10-07 67 views
6

我正在寫一個rake任務,每分鐘(可能在將來每30秒)調用一次,並且它會聯繫輪詢API端點(每個用戶在我們的數據庫中)。很明顯,這不是單線程運行效率高,但可以多線程嗎?如果沒有,是否有一個好的基於事件的HTTP庫可以完成工作?多線程rake任務

回答

12

我正在寫一耙的任務,將被稱爲由每當

當心的Rails的啓動時間每分鐘(在未來,每30秒有可能),它可能是最好使用一個分叉模型,如Resque或Sidekiq,Rescue提供https://github.com/bvandenbos/resque-scheduler這應該能夠做你所需要的,我不能談論Sidekiq,但我確定它有類似的可用(Sidekiq比Resque更新)

很明顯,這不是單線程運行效率高,但有可能多線程?如果沒有,是否有一個好的基於事件的HTTP庫可以完成工作?

我建議你看一下ActiveRecord's find_each爲讓您的取景過程更加高效,技巧,一旦你有你批你可以很容易地使用線程,如做一些事情:

# 
# Find each returns 50 by default, you can pass options 
# to optimize that for larger (or smaller) batch sizes 
# depending on your available RAM 
# 
Users.find_each do |batch_of_users| 
    # 
    # Find each returns an Enumerable collection of users 
    # in that batch, they'll be always smaller than or 
    # equal to the batch size chosen in `find_each` 
    # 
    # 
    # We collect a bunch of new threads, one for each 
    # user, eac 
    # 
    batch_threads = batch_of_users.collect do |user| 
    # 
    # We pass the user to the thread, this is good 
    # habit for shared variables, in this case 
    # it doesn't make much difference 
    # 
    Thread.new(user) do |u| 
     # 
     # Do the API call here use `u` (not `user`) 
     # to access the user instance 
     # 
     # We shouldn't need to use an evented HTTP library 
     # Ruby threads will pass control when the IO happens 
     # control will return to the thread sometime when 
     # the scheduler decides, but 99% of the time 
     # HTTP and network IO are the best thread optimized 
     # thing you can do in Ruby. 
     # 
    end 
    end 
    # 
    # Joining threads means waiting for them to finish 
    # before moving onto the next batch. 
    # 
    batch_threads.map(&:join) 
end 

這將不啓動超過batch_size的線程,等待每個batch_size完成。

有可能做這樣的事情,但是接下來你將有不可控制的線程數量,還有一種選擇可能會從這裏受益,它會變得更加複雜,包括ThreadPool和共享工作列表做,我已經發布它作爲在Github so'as沒有垃圾郵件計算器:https://gist.github.com/6767fbad1f0a66fa90ac

+0

ThreadPool看起來不錯!我會試試 –

+0

好東西,謝謝:) – Robin

3

我會建議使用sidekiq這是很好的多線程。然後,您可以爲每個用戶排隊單獨作業以輪詢API。 clockwork可用於使您入職的工作重複發生。