开发者

ruby: How do i get the number of subprocess(fork) running

I want to limit the subprocesses count to 3. Once it hi开发者_JS百科ts 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?


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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜