简体   繁体   中英

How to run two methods in parallel ruby

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?

Use thread.

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

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.

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

Use fork to run things in parallel, threads are concurrent but not parallel in ruby due to the global interpreter lock which processes one thread at a time.

https://www.toptal.com/ruby/ruby-concurrency-and-parallelism-a-practical-primer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM