关于Ruby forking的快速问题-我之前在Resque中遇到了一些forking代码,非常性感,但却让我被绊倒了几次。
我希望有人能给我更多关于这里发生的事情的细节。具体地说--似乎派生了一个子代(预期的),并将其直接踢入我的条件的“else”端(较少预期的。这是预期的行为吗?Ruby成语?
我的IRB技巧如下:
def fork
return true if @cant_fork
begin
if Kernel.respond_to?(:fork)
Kernel.fork
else
raise NotImplementedError
end
rescue NotImplementedError
@cant_fork = true
nil
end
end
def do_something
puts "Starting do_something"
if foo = fork
puts "we are forking from #{Process.pid}"
Process.wait
else
puts "no need to fork, let's get to work: #{Process.pid} under #{Process.ppid}"
puts "doing it"
end
end
do_something发布于 2010-12-25 06:47:06
这就是fork的工作原理。从文档中:
否则,fork调用返回两次,一次在父级,返回子进程的进程ID,一次在子级,返回nil。
因此,在父进程中'foo = fork‘是子进程的PID,在子进程中'foo = fork’是零,因此它在子进程中使用'else‘分支。
https://stackoverflow.com/questions/4528590
复制相似问题