How to run two methods in parallel ruby

11,302

Solution 1

Use thread.

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

Solution 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.

Share:
11,302
Hu Man
Author by

Hu Man

Updated on June 04, 2022

Comments

  • Hu Man
    Hu Man almost 2 years

    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?

  • Hu Man
    Hu Man about 11 years
    Thank you for your reply, i've tried it but it hangs in second process
  • David Unric
    David Unric about 11 years
    @HuMan It does work with #join . Just read docs what this method does ruby-doc.org/core-1.9.3/Thread.html#method-i-join