如何在并行ruby中运行两种方法?

时间:2022-05-19 13:52:05

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?

我有两个方法。第一个远程执行可执行文件,第二个则与可执行文件对话。可执行文件是一个web服务。第一步不返回true(通过shell执行),因为它开始并等待第二步。有没有办法并行地执行第一个方法和第二个方法?

3 个解决方案

#1


3  

Use thread.

使用线程。

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

#2


4  

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

除了股票线程支持之外,我还想提一下伟大的Ruby gem并行

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

它可以并行地生成进程,同时使用多个cpu /内核。

#3


0  

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.

您可以使用ruby的线程来实现这一点。您可以检查这些链接,以便了解如何使用线程。

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

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

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

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

#1


3  

Use thread.

使用线程。

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

#2


4  

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

除了股票线程支持之外,我还想提一下伟大的Ruby gem并行

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

它可以并行地生成进程,同时使用多个cpu /内核。

#3


0  

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.

您可以使用ruby的线程来实现这一点。您可以检查这些链接,以便了解如何使用线程。

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

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

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

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