perl 进程队列 [英] perl process queue

查看:26
本文介绍了perl 进程队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Perl 脚本,它分叉了许多子进程.我想要某种功能,例如 xargs --max-procs=4 --max-args=1make -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.

很容易说 fork 四个进程并等待它们全部完成,然后再 fork 另一个四个,但我想保持四个或 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆