perl进程队列 [英] perl process queue
问题描述
我有一个Perl脚本,该脚本派生了许多子进程.我想要某种功能,例如xargs --max-procs=4 --max-args=1
或make -j 4
,在这些功能中,Perl将保持给定数量的进程运行,直到用尽为止.
I have a Perl script which forks a number of sub-processes. I'd like to have some kind of functionality like xargs --max-procs=4 --max-args=1
or make -j 4
, where Perl will keep a given number of processes running until it runs out of work.
很容易说分叉四个进程并等待它们全部完成,然后分叉另外四个进程,但是我想让四个或n个进程同时运行,并在一个进程完成后立即分叉一个新进程.
It's easy to say fork four process and wait for them all to complete, and then fork another four, but I'd like to keep four or n processes running at the same time, forking a new process as soon as one completes.
Perl中是否有一种简单的方法来实现这样的进程池?
Is there a simple way in Perl to implement such a process pool?
推荐答案
Forks::Super
可以满足此要求.
Forks::Super
can handle this requirement.
use Forks::Super MAX_PROC => 5, ON_BUSY => [ block | queue ];
对fork()
的调用可以阻塞,直到活动子进程的数量降至5以下为止,或者可以将其他参数传递给fork
调用,并且要执行的任务可能会排队:
Calls to fork()
can block until the number of active subprocesses falls below 5, or you can pass additional parameters to the fork
call and the tasks to perform can queue up:
fork { sub => sub { ... task to run in subprocess ... } }
一个子进程完成后,队列中的另一作业将启动.
When one subprocess finishes, another job on the queue will start up.
(我是本单元的作者).
(I am the author of this module).
这篇关于perl进程队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!