1 つのアプローチは、これに Thread::Queue を使用することです。すべてのスレッドが情報を結果キューにプッシュ (エンキュー) し、メイン コードで処理します。
sub start {
my @result;
$queue= Thread::Queue->new;
$queue_processed = Thread::Queue->new;
my @domains = get_domains($domains_filename);
$queue->enqueue(@domains);
my @threads= map { threads->create( sub { create_thread($_) } ) } ( 1 .. $CONFIG{NUMBER_OF_THREADS} );
$_->detach for (@threads);
my $counter = 0;
while ( $counter < scalar @domains ) {
my $result = $queue_processed->dequeue_nb;
if ($result) {
if ( $result->{status} ) {
$txt_processed_domains->configure(-state => "normal");
$txt_processed_domains->insert_end( $result->{domain} . ".com" . " => " . "Available!" );
$txt_processed_domains->see("end");
$txt_processed_domains->configure(-state => "disabled");
Tkx::update();
$counter++;
Win32::Sound::Volume('100%');
Win32::Sound::Play( $CONFIG{SOUND_FILE} );
Win32::Sound::Stop();
my $response = Tkx::tk___messageBox( -type => "yesno", -message => $result->{domain} . ".com" . " is " . "Available! Continue?", -icon => "question", -title => "Domain found" );
unless ( $response eq 'yes' ) {
exit;
}
}
else {
$txt_processed_domains->configure(-state => "normal");
$txt_processed_domains->insert_end( $result->{domain} . ".com" . " => " . "Already taken!" );
$txt_processed_domains->itemconfigure( $counter, -background => "#f0f0ff" );
$txt_processed_domains->see("end");
$txt_processed_domains->configure(-state => "disabled");
Tkx::update();
$counter++;
}
}
}
Tkx::tk___messageBox( -message => "Completed!" );
}
sub create_thread {
my $thread_id = shift;
my ($domain);
while( $domain = $queue->dequeue_nb ) {
my $mech = MyMech->new( autocheck => 1 );
$mech->quiet(0);
$mech->get( $CONFIG{BASE_URL} . "domains/search.aspx?domainToCheck=$domain&tld=..com" );
if ( $mech->content() =~ m{is\s+available!}is ) {
open my $fh, ">>", $result_filename or die "Couldn't create result file! $!";
#$queue_processed->enqueue( "$domain.com => Available!" );
$queue_processed->enqueue( { status => 1, domain => $domain, } );
print $fh "$domain.com\n";
close $fh;
}
else {
$queue_processed->enqueue( { status => 0, domain => $domain, } );
}
#sleep $CONFIG{DELAY_BETWEEN_REQUESTS};
}
return 1;
}