Python Ruby中subprocess.Popen()的等价物?

Python Ruby中subprocess.Popen()的等价物?,python,ruby,Python,Ruby,Ruby中是否有subprocess.Popen()的等价物?我必须并行运行一些程序并重定向它们的I/O。 差不多 p = subprocess.Popen("python foo.py", stdin=inFile, stdout=outFile, stderr=outFile) 更新:subprocess.Popen是一个Python类,允许并行执行进程。第一个参数是要执行的语句。它可以是一个简单的“ls”命令,也可以是编译C程序的命令。stdin、stdout和stderr参数将文件作为

Ruby中是否有subprocess.Popen()的等价物?我必须并行运行一些程序并重定向它们的I/O。 差不多

p = subprocess.Popen("python foo.py", stdin=inFile, stdout=outFile, stderr=outFile)
更新:subprocess.Popen是一个Python类,允许并行执行进程。第一个参数是要执行的语句。它可以是一个简单的“ls”命令,也可以是编译C程序的命令。stdin、stdout和stderr参数将文件作为参数,用于重定向可能使用它运行的程序的输入和输出

将非常有用

假设您有一个
test.rb
文件,如下所示:

v = gets.chomp
puts "#{v} @ #{Time.new}"
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
你可以做:

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

对于要并行执行的多个子进程,可以使用如下所示的线程:

v = gets.chomp
puts "#{v} @ #{Time.new}"
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

如果您对其他选项不满意,那么python的
子流程
库有一个ruby端口,由Stripe开源

它具有
popen
功能,例如

Subprocess.popen(
  ['python', 'foo.py'],
  stdin: inFile,
  stdout: outFile, 
  stderr: outFile)

如果您解释
subprocess.Popen()
的作用,这将有所帮助。否则,您会人为地将能够回答您问题的人员限制为在和方面都非常熟悉流程管理的人员。这能并行运行吗?也就是说,我可以并行运行多个Open3.popen3实例,而不必等待1完成吗?第二,我能用这个编译程序吗?类似于“gcc foo.cpp”。@Maruthgoyal不确定平行部分。汇编应fine@Maruthgoyal您将不得不使用线程进行并行执行,非常感谢!我要试一试