How to run two methods in parallel ruby

老子叫甜甜 提交于 2019-12-21 20:19:47

问题


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



来源:https://stackoverflow.com/questions/15782492/how-to-run-two-methods-in-parallel-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!