ruby:如何获取正在运行的子进程(fork)的数量

发布于 2024-09-09 07:35:06 字数 117 浏览 1 评论 0原文

我想将子进程数限制为 3。一旦达到 3,我就会等到其中一个进程停止,然后执行一个新进程。我正在使用 Kernel.fork 来启动该过程。

如何获取正在运行的子进程的数量?或者有更好的方法来做到这一点吗?

I want to limit the subprocesses count to 3. Once it hits 3 i wait until one of the processes stops and then execute a new one. I'm using Kernel.fork to start the process.

How do i get the number of running subprocesses? or is there a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南渊 2024-09-16 07:35:06

这是一个很好的问题,但我认为 Ruby 中没有这样的方法,至少在标准库中没有。那里有很多宝石......

这个问题听起来像是 Mutex 类的工作。请在此处查找条件变量部分,了解如何使用 Ruby 的互斥体。

A good question, but I don't think there's such a method in Ruby, at least not in the standard library. There's lots of gems out there....

This problem though sounds like a job for the Mutex class. Look up the section Condition Variables here on how to use Ruby's mutexes.

渡你暖光 2024-09-16 07:35:06

我通常有一个要完成的任务队列,然后有几个线程消耗任务,直到它们收到指示工作结束的项目。 Thread 库下的“Programming Ruby”中有一个示例。 (我不确定是否应该将示例复制并粘贴到 Stack Overflow - 抱歉)

I usually have a Queue of tasks to be done, and then have a couple of threads consuming tasks until they receive an item indicating the end of work. There's an example in "Programming Ruby" under the Thread library. (I'm not sure if I should copy and paste the example to Stack Overflow - sorry)

梦冥 2024-09-16 07:35:06

我的解决方案是使用 trap("CLD"),每当子进程结束时捕获 SIGCLD 并减少正在运行的进程的计数器(全局变量)。

My solution was to use trap("CLD"), to trap SIGCLD whenever a child process ended and decrease the counter (a global variable) of processes running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文