How to limit the maximum number of parallel threads in perl

I have a program (Perl) that launches a huge number of threads (each of which is responsible for creating graphics based on data processing). Every thread that I start using:

my @threads //list to store threads that have been launched

push @threads,threads->create(\mySubName,params...);

Streams work correctly, but some time after I opened several of them, Perl-interpreter errors (I suppose, is this related to memory?). So my solution is to limit the number of threads that I open at a time, I chose 15. And I want to add a sub in front of each line to check whether it is normal to start the next thread or to sleep, while I wait for to finish. This is how I tried to do this.

sub checkThreads{
    my $addThread = 0;
    until($addThread){
        my $totalThreads = 0;
        foreach my $task (@threads){
            if($task->is_running()){$totalThreads++;}
        }
        if($totalThreads <= 15 ){
            print "Ok to add new thread, carry on!\n";
            $addthread = 1;
        }else{
            print "Waiting for $totalThreads threads to fire next one...\n";
            sleep 2;
        }
    }
}

So every time I want to create a new thread, I just call

&checkThreads;

, , . , sub, , , :

$task->is_running()

- . , , .

?

, , :

scalar(threads->list());

, , , :

threads=SCALAR(0x80fea8c)
+5
3

Thread::Semaphore concurrency:

my $sem = Thread::Semaphore->new(15); # max 15 threads
my @threads = map {
    # request a thread slot, waiting if none are available:
    $sem->down;
    threads->create(\&mySubName, @params)
} 0..100;
$_->join for @threads;

:

sub mySubName {
    do_stuff();
    # release slot:
    $sem->up;
}
+5

,

my $count = threads->list();

, , . , ? , .

my $count = () = threads->list();
+1
man perlthrtut

What Threads Are Running?
   "threads->list()" returns a list of thread objects, one for each thread
    that currently running and not detached.  Handy for a number of

In other words, find out how many items in the list are returned by thread-> list (), and you have an account.

You might want to examine Thread :: Pool or other cpan packages to see if someone else has made a hard climb for you.

0
source

All Articles