1
私のテストでは、いくつかのSQLクエリを別のスレッドで実行する際に問題があります。私は、単純なselectとcountをSQLで実行すると同じ結果を返すと予想しますが、これらのメソッドをスレッド(process_new_followers
とprocess_lost_followers
)で実行すると、異なる結果が返されます。RubyマルチスレッドSQL(テスト環境)
def compute_followers
followers = Follower.where({ user_id: user_id, is_following: true }).count # => returns 10 always
calculate_differences
thread1 = Thread.new { process_new_followers }
thread2 = Thread.new { process_lost_followers }
thread1.join
thread2.join
end
def process_new_followers
puts Follower.where({ user_id: user_id, is_following: true }).count # => returns 0 if threaded and 10 if not
... I commented out this code so it only prints the count and nothing changes, so no db changes are happening in this method
end
def process_lost_followers
puts Follower.where({ user_id: user_id, is_following: true }).count # => returns 0 if threaded and 10 if not
... I commented out this code so it only prints the count and nothing changes, so no db changes are happening in this method
end
なぜスレッディング効果は、このSQLクエリになります。ここでは
は(基本的な)コードのですか?