Problem with perl threading

I am writing a multi-threaded website health check in perl, and here is the basic code so far (includes only the stream part):

#!/usr/bin/perl

use LWP::UserAgent; 
use Getopt::Std; 
use threads; 
use threads::shared; 

my $maxthreads :shared = 50;
my $threads :shared = 0;

print "Website Uptime Checker\n";
my $infilename = $ARGV[0];
chomp($infilename);
open(INFILE, $infilename);
my $outfilename = $ARGV[1];
chomp($outfilename);
open(OUTFILE, ">" . $outfilename);
OUTFILE->autoflush(1);
while ($site = <INFILE>)
{
    chomp($site);
    while (1)
    {
        if ($threads < $maxthreads)
        {
            $threads++;
            my $thr = threads->create(\&check_site, $site);
            $thr->detach();
            last;
        }
        else
        {
            sleep(1);
        }
    }
}
while ($threads > 0)
{
    sleep(1);
}

sub check_site
{
    $server = $_[0];
    print "$server\n";
    $threads--;
}

After a while, a message is displayed:

It is not possible to call the "disconnect" method undefined on line C: \ perl \ webchecker.pl 28, line 245.

What causes this error? I know this is in isolation, but what am I doing wrong in my code? Windows shows a lot of free memory, so there should not be enough memory on the computer, this error occurs even if I install $ maxthreads at least 10 or possibly even lower.

+3
source share
1 answer

, thread->create undef. thr detach, , .

+1

Source: https://habr.com/ru/post/1749854/


All Articles