How to run two methods in parallel ruby

2020-07-18 03:29发布

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?

3条回答
Evening l夕情丶
2楼-- · 2020-07-18 03:57

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

查看更多
We Are One
3楼-- · 2020-07-18 04:02

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.

查看更多
萌系小妹纸
4楼-- · 2020-07-18 04:24

Use thread.

t1 = Thread.new do
  first_method
end
second_method
t1.join
查看更多
登录 后发表回答