在 Perl 5 中,如何在达到最大限制后对进程/应用程序进行排队 [英] In Perl 5, how to queue a process/application after it reaches a maximum limit

查看:40
本文介绍了在 Perl 5 中,如何在达到最大限制后对进程/应用程序进行排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序被符号链接到多个目录,例如

I have a program that is being symlinked to multiple directories e.g.

/main/foo.pl
/run1/foo.pl ->/main/foo.pl
/run2/foo.pl ->/main/foo.pl
/run3/foo.pl ->/main/foo.pl
/run4/foo.pl ->/main/foo.pl

/main/foo.pl
/run1/foo.pl -> /main/foo.pl
/run2/foo.pl -> /main/foo.pl
/run3/foo.pl -> /main/foo.pl
/run4/foo.pl -> /main/foo.pl

它们作为 cron 作业运行,因此我在 crontab 中有以下条目:

They're being run as cron jobs, hence I have the following entries in the crontab:

*/2 * * * */run1/foo.pl
*/2 * * * */run2/foo.pl
*/2 * * * */run3/foo.pl
*/2 * * * */run4/foo.pl

*/2 * * * * /run1/foo.pl
*/2 * * * * /run2/foo.pl
*/2 * * * * /run3/foo.pl
*/2 * * * * /run4/foo.pl

foo.pl 的一个片段如下:

use Fcntl qw(:flock);
use autodie qw(:all);
open my $self, '>', "$FindBin::Bin/lockme";
flock( $self, LOCK_EX|LOCK_NB )
    or die "Cannot acquire lock, already running!";

{
    my $long_proc = Process->new();
    $long_proc->run();
}        

你明白了,每个 cron 进程只能运行一次,因为有一个锁信号量检查.但是run1、run2、run3、run4可以同时运行.

You get the idea, each of the cron process can only run once because there is a lock semaphore check. But run1, run2, run3, and run4 can run simultaneously.

现在我需要的是将进程数限制为最多四个.如果有人添加了另一个 cron 进程,例如:

Now what I need is that I want to limit the number of process to maximum of four. If someone adds another cron processes like:

新符号链接:

/run5/foo.pl ->/main/foo.pl
/run6/foo.pl ->/main/foo.pl

/run5/foo.pl -> /main/foo.pl
/run6/foo.pl -> /main/foo.pl

额外的 crontab:

Additional crontab:

*/5 * * * */run5/foo.pl
* * * * */run6/foo.pl

*/5 * * * * /run5/foo.pl
* * * * * /run6/foo.pl

只要 run1、run2、run3 和 run4 仍在运行,run5 和 run6 都需要排队.因此,在任何给定时间将只有 4 个进程在运行.

Both run5 and run6 need to be queued whenever run1, run2, run3, and run4 are all still running. Thus at any given time there will be only 4 processes run.

我怎样才能做到这一点?有没有处理它的 CPAN 模块?

How can I achieve that? Is there any CPAN module that handles it?

谢谢!

推荐答案

请参阅上面@pilcrow 回答中来自@tsee 的评论.Steffen Muller 的IPC::ConcurrencyLimit 可以很好地控制协同多进程的并发进程数.

Please see comment from @tsee in @pilcrow answer above. Steffen Muller's IPC::ConcurrencyLimit can control the number of concurrenct processes on cooperative multi processing nicely.

这篇关于在 Perl 5 中,如何在达到最大限制后对进程/应用程序进行排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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