请帮助我将我的脚本改为多线程。我已经阅读了threads::shared
模块的文档,但是并没有帮助我理解如何实现。
use threads;
use threads::shared;
use LWP::UserAgent;
use HTTP::Cookies;
my $NUM_WORKERS = 2;
sub worker {
my ($i) = @_;
my ($web, $ck) = browser();
($username, $password) = split ':', $acc;
my $url = 'http://www.site.ru/?tkn'. int(rand(10000));
my $response = $web->post($url, Content =>
[//////]);
while(1)
{
my $url = 'http://www.site.ru/dk?st.page='.$i.'&st.name=%D0%B0';
my $response = $web->get($url);
@list = ($response->content =~ /card_wrp"><div class="photoWrapper"><div><a href="\/(.*?)\?/g);
@popl = ($response->content =~/<\/div><div class="info">(.*?)<\/div>/g);
for ($j = 0; $j <= scalar @list - 1; $j++)
{
$popl[$j] =~ s/ //g;
open F, ">>gr.txt";
print F $list[$j].':'.$popl[$j]. "\n";
close F;
}
print "[+] Page $i \n";
}
}
my $i :shared = 1;
my $last = 79265;
my @workers;
for (1..$NUM_WORKERS) {
push @workers, async {
while (1) {
my $i;
{
lock $I;
return if $i == $last;
$i = ++$I;
}
worker($i);
}
};
}
$_->join() for @workers;
sub browser
{
my $web = new LWP::UserAgent;
my $ck = new HTTP::Cookies;
$web->cookie_jar($ck);
$web->agent('Opera/9.80 (Windows 7; U; en) Presto/2.9.168 Version/11.50');
$web->requests_redirectable(0);
$web->timeout(5);
return $web, $ck;
}
sub loadf {
open (F, "<".$_[0]) or erroropen($_[0]);
chomp(my @data = <F>);
close F;
return @data;
}
我不知道哪些变量需要共享。感谢所有愿意帮助我的成员。