【发布时间】:2012-08-28 22:35:30
【问题描述】:
我写了一个代码,我需要让它成为多线程的。 Evething 有效,但每个循环重复 4 次:
use LWP::UserAgent;
use HTTP::Cookies;
use threads;
use threads::shared;
$| = 1;
$threads = 4;
my @groups :shared = loadf('groups.txt');
my @thread_list = ();
$thread_list[$_] = threads->create(\&thread) for 0 .. $threads - 1;
$_->join for @thread_list;
thread();
sub thread
{
my $url = 'http://www.site.ru/';
my $response = $web->post($url, Content =>
['st.redirect' => ''
]);
foreach $i (@groups)
{
my $response = $web->get($i);
if(!($response->header('Location')))
{
---------;
}
else
{
----------;
}
}
}
sub loadf {
open (F, "<".$_[0]) or erroropen($_[0]);
chomp(my @data = <F>);
close F;
return @data;
}
groups.txt:
http://www.odnoklassniki.ru/group/47357692739634
http://www.odnoklassniki.ru/group/56099517562922
我知道我需要使用threads::shared;但我无法理解如何使用它。
您的帖子没有太多上下文来解释代码部分;请更清楚地解释您的情况。
【问题讨论】:
-
你期望什么行为,你得到了什么?您是否可以删除代码中不重要的部分以提供一个最小的示例?见sscce.org
标签: multithreading perl