gpt4 book ai didi

ruby - Process::detach 和 Process::wait 是否互斥(Ruby)?

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:57 25 4
gpt4 key购买 nike

我正在重构我的 Ruby on Rails 服务器(在 Linux 上运行)中的一些并发处理以使用 Spawn。 Spawn::fork_it 文档声称 fork 进程在分离后仍然可以等待:https://github.com/tra/spawn/blob/master/lib/spawn.rb (第 186 行):

# detach from child process (parent may still wait for detached process if they wish)
Process.detach(child)

但是,Ruby Process::detach 文档说你不应该这样做:http://www.ruby-doc.org/core/classes/Process.html

Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait(). If the parent never collects this status, the child stays around as a zombie process. Process::detach prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach only when you do not intent to explicitly wait for the child to terminate.

然而 Spawn::wait 有效地允许您通过包装 Process::wait 来做到这一点。另外,我特别想使用 Process::waitpid2 方法来等待子进程,而不是使用 Spawn::wait 方法。

detach-and-wait 是否不能在 Linux 上正常工作?我担心这可能会导致分离的收割者线程和等待的父进程之间的竞争条件,至于谁先收集子状态。

最佳答案

这个问题的答案在文档中。您是否在受控环境中编写供自己使用的代码?还是被第三方广泛使用? Ruby 是为第三方广泛使用而编写的,因此他们的建议是不要做可能在“某些操作系统”上失败的事情。也许 Spawn 库主要是为在 Linux 机器上使用而设计的,并且只在这种策略起作用的一小部分上进行了测试。

如果您要分发您正在编写的代码供所有人使用,我会采用 Ruby 的方法。

如果您控制运行此代码的环境,我会编写两个测试:

  1. 生成进程、分离进程然后等待进程的测试。
  2. 生成一个进程然后等待它的测试。

计算两者的失败率,如果它们相等(在您认为可以接受的范围内),那就去做吧!

关于ruby - Process::detach 和 Process::wait 是否互斥(Ruby)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7591140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com