Ruby 运行 两个多线程脚本

Ruby running two scripts with mulithreading

所以我试图让我的 ruby(没有 rails)应用程序成为 运行 与来自终端的单个调用,即 'ruby run.rb'。但是我有两个脚本需要 运行、app.rbapp2.rb,问题是,这两个脚本都没有完成 - 它们继续 运行 以保持系统 运行ning,这意味着其中一个脚本永远不会 运行 - 它调用第一个脚本 (app.rb) 而不是第二个 (app2.rb) 这些脚本需要同时 运行!

当我打开另一个命令行并且每个命令行中只有 运行 一个脚本时它确实有效。

我试过:

def runApp
    system("ruby app.rb")
end
def runApp2
    system("ruby app2.rb")
end
t1 = Thread.new{runApp()}
t2 = Thread.new{runApp2()}
t1.join
t2.join

然而,这只会 运行 第一个线程(第一个 运行ning app.rb),因为它一直在 运行。任何想法如何也可以同时 运行 第二个线程?

:编辑:其中一个脚本正在使用 Sinatra gem,另一个也每十秒调用一次它的函数。

所以我找到的一种可能的解决方案是

system("ruby app.rb & ruby app2.rb")

这仅在 运行 来自 linux 的情况下才有效,但是我仍然希望有任何进一步的解决方案。

根据 documentation 你可以这样做:

threads = []
threads << Thread.new{runApp()}
threads << Thread.new{runApp2()}

threads.each { |thr| thr.join }

我猜这是可行的,因为 each 是并行的。