当在产生的过程中使用睡眠时,它会杀死该过程

时间:2016-02-16 00:41:47

标签: ruby multithreading rspec

我主要是一个修补Process.spawn的java人,并且打了一个墙。基本上我把几个rspec类放在一起,然后修补它。这非常有效。然后我睡了一觉,整个过程就在睡觉时就死了。我不知道为什么,我认为产卵会解决线程的共享资源问题。以下是我用来制作流程的内容

x1 = runif(1000)
x2 = runif(1000)
x3 = x1 + x2
x4 = runif(1000)
x5 = runif(1000)*0.00000001 +x4
x6 = x5 + x3
x = data.frame(x1, x2, x3, x4, x5, x6)

const = rep(1,1000)
a<-lm(const ~ ., data=x)
names(a$coefficients[!is.na(a$coefficients)])[c(-1)]

规范中的测试很少,下面是一个样本。

test_files = Dir["../spec/*.rb"]
test_files.each do |file|
  pid = Process.spawn("rspec #{file}")
  p pid
end
Process.wait

有人可以这么好解释我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

它对我有用,我假设你认为它不起作用,因为你得到这样的东西:

script.rb:6:in `wait': No child processes (Errno::ECHILD)
from script.rb:6:in `<main>'

我假设你得到了这个,因为你可能在lib目录中有这个脚本,但是你从应用程序的根目录运行它,所以当它Dir["../spec/*.rb"]时你期望它上升到你的应用程序的根目录,然后进入spec目录,但它是基于你的pwd,所以它会转到应用程序的父目录并查找一个spec目录,它不存在。

我在这种情况下提倡的一些事情,在尝试生成文件之前检查它是否找到了文件。使用绝对文件路径,除非您有意希望它基于您运行程序的位置。如果这是真的,请尝试这样写:

spec_glob  = File.expand_path('../spec/*.rb', __dir__)
test_files = Dir[spec_glob]
p test_files
test_files.each do |file|
  pid = Process.spawn("rspec #{file}")
  p pid
end
Process.wait