Ruby中subprocess.Popen()的等价物?
在Ruby中是否有等价的subprocess.Popen()?我必须并行运行一些程序并重定向它们的I/O。 喜欢的东西Ruby中subprocess.Popen()的等价物?
p = subprocess.Popen("python foo.py", stdin=inFile, stdout=outFile, stderr=outFile)
UPDATE:subprocess.Popen是一个Python类,允许的处理并行执行。第一个参数是要执行的语句。它可能是一个简单的'ls'命令或编译C程序的命令。 stdin,stdout和stderr参数将文件作为参数来重定向您可能使用它运行的程序的输入和输出。
Open3#popen3将是有用的。
假设你有一个test.rb
文件,如下图所示:
v = gets.chomp
puts "#{v} @ #{Time.new}"
你可以这样做:
require "open3"
stdin, stdout, stderr, wait_thr = Open3.popen3("ruby test.rb")
stdin.puts("hi")
puts stdout.gets(nil)
#=> hi @ 2016-02-05 19:18:52 +0530
stdin.close
stdout.close
stderr.close
对于多个子进程并行执行,你可以使用线程作为如下所示:
require "open3"
t1 = Thread.new do |t|
stdin, stdout, stderr, wait_thr1 = Open3.popen3("ruby test.rb")
stdin.puts("Hi")
puts stdout.gets(nil)
end
t2 = Thread.new do |t|
stdin, stdout, stderr, wait_thr1 = Open3.popen3("ruby test.rb")
stdin.puts("Hello")
puts stdout.gets(nil)
end
t1.join
t2.join
2件事。这可以并行运行吗?那就是我可以并行运行多个Open3.popen3实例而无需等待1完成?其次,我可以使用它编译程序吗?像“gcc foo.cpp”。 –
@Maruthgoyal不确定平行部分。编译应该没问题 –
@Maruthgoyal你将不得不使用线程来执行并行执行 –
它会帮助如果你解释一下'subprocess.Popen()'是做什么的。否则,您可以人为地将能够回答您问题的人员限制在非常熟悉* [tag:ruby]和[tag:python]中的流程管理的人员的交集处。 –