I am not sure if I am doing this right, but here is my scenario: I need to create a thread to do some API call and continue with normal operations while the API call is made. This part is fine. The issue arises when I want to join back in. I am currently creating a thread in a different module and want to join back in a different module. Hence I am not able to use the reference to the thread created earlier.
To overcome this I did this: Thread.current[:ref_to_new_thread] = Thread.new { API CALL }
Finally I join back in a different module using - Thread.current[:ref_to_new_thread].join
Is this the right way? or is there a better way.
Minimal reproducible example:
File 1:
module A
module Aa
def my_method
#some actions
Thread.current[:ref_to_new_thread] = Thread.new { API CALL }
#some actions
end
end
end
File 2:
module B
module Bb
def my_second_method
#some actions
Thread.current[:ref_to_new_thread].join
Thread.current[:ref_to_new_thread] = nil
end
end
end
I am new to ruby on rails so my apologies.