Subject: | Thread pool not working with more than 256 worker threads |
Hi Elizabeth,
This is with regard to an error which I encountered while using thread pool.
I am pasting the error which I got when I increased thread count to 260.
==================================================================
Can't call method "tid" on an undefined value at
/usr/lib/perl5/site_perl/5.8.8/Thread/Pool.pm (loaded on demand from
offset 13517 for 1559 bytes) line 526
==================================================================
Please help me as I am stuck in the middle of my project. Its very urgent.
Thanks and Regards,
Geo Varghese
Subject: | program.pl |
#!/usr/bin/perl
use strict;
use DBI;
use Thread::Pool;
# my %resolved : shared;
# Create the pool of threads
my $pool = Thread::Pool->new(
{
workers => 255,
maxjobs => 500,
do => \&check_service,
monitor => \&monitor,
}
);
my $res = 1; # type of service check
my $dbh = db_connect();
my $sql = "select * from target where resolution=$res order by userid";
my $sth = $dbh->prepare($sql);
$sth->execute();
my $i = 0;
while (my $row = $sth->fetchrow_hashref ) {
# $pool->add();
my $query = 'select * from check_'.$row->{"check_type"}.' where targetid='.$row->{"targetid"};
my $st = $dbh->prepare($query);
$st->execute();
while (my $data = $st->fetchrow_hashref ) {
if($i++ > 10){
$pool->add(1);$i=0;
}
# check_service('check_'.$row->{"check_type"}, $data->{'url_ip'}, $row->{"userid"}, $row->{"checkname"}, $row->{"targetid"});
$pool->job('check_'.$row->{"check_type"}, $data->{'url_ip'}, $row->{"userid"}, $row->{"checkname"}, $row->{"targetid"});
}
$st->finish;
}
$dbh->disconnect;
#function to check services
sub check_service{
my($script, $url_ip, $userId, $checkName, $targetId) = @_ ;
my $command = "./checks/$script -H $url_ip";
my $out = `./checks/$script -H $url_ip -t 30 2>&1`;
my $status = "";
my $responseTime = 30;
my $error = "";
if($out =~ /time=(\d+\.\d+)s/){
$responseTime = $1;
}
# parsing out put
if($out =~ "OK"){
$status = "OK";
}elsif($out =~ "CRITICAL"){
$status = "CRITICAL";
$out =~ /CRITICAL - (.*)$/;
$error = $1;
}elsif($out =~ "WARNING"){
$status = "WARNING";
$out =~ /WARNING(.*)$/;
$error = $1;
}else{
$status = "CRITICAL";
$error = $out;
}
print $out."\n";
my $sql = qq|insert into reports values('', '$userId', '$targetId', '$checkName', now(), '$status','$responseTime','$error')|;
my $new_dbh = db_connect();
my $new = $new_dbh->prepare($sql);
$new->execute();
my $rc = $new_dbh->disconnect;
}
sub db_connect(){
# connect to database
my $user = "monitor";
my $password = "xxxx";
my $host = "localhost";
my $driver= "mysql";
my $db = "monitor";
my $dsn = "DBI:$driver:database=$db;host=$host";
my $new_dbh = DBI->connect($dsn, $user, $password);
return $new_dbh;
}