How to run two methods in parallel ruby

2020-07-18 03:42发布

问题:

I have two methods. The first one remotely executes an executable and the second one stars talking with an executable. The executable is a web-service. The first step does not return the true (executed through shell) because it starts and waits for the second step. Is there a way to execute the first method and the second method in parallel?

回答1:

Use thread.

t1 = Thread.new do
  first_method
end
second_method
t1.join


回答2:

Besides the stock threads support I'd like to mention the great Ruby gem Parallel

It can spawn processes in parallel and utilize multiple CPUs/cores at the same time.



回答3:

you can use ruby's threads to do that. You can check out the links so you can have an ideia of what you can do with threads.

http://www.tutorialspoint.com/ruby/ruby_multithreading.htm

http://ruby-doc.org/core-2.0/Thread.html