所以我最近想要通過一個Perl程序來提高速度。考慮到網站列表,我想爲每個網址開始一個線索並獲取每個網站的內容,然後在頁面上查找公司描述。一旦一個線程找到了結果,或者所有線程都沒有,我想退出,寫下我的結果,然後閱讀我的下一個公司的URL。Perl線程和不安全信號
我看到的問題是我在創建線程時調用的函數內部使用了Perl :: Unsafe :: Signals模塊。我需要不安全的信號來中斷正在被卡住的正則表達式。但是,這似乎會導致各種各樣的問題,主要是程序崩潰,並顯示錯誤消息「鬧鐘」。
因此,有沒有一種方法可以安全地使用Perl :: Unsafe :: Signals和線程?有沒有辦法通過向函數發送信號以另一種方式超時正則表達式(例如,我在下面發送'KILL'信號?)謝謝。
注:我將代碼分解到所有相關部分,讓我知道你是否需要更多。
use threads ('exit' => 'threads_only');
use threads::shared;
my @descrip;
share(@descrip);
my $lock;
share($lock);
URL:foreach my $url(@unique_urls) {
#skip blank urls
if(!$url) { next URL; }#if
#find description
my $thread = threads->create(\&findCompanyDescription, $PREV_COMPANY, $PREV_BASE_URL, $url);
#while a description has not been found and there are still active threads, keep looking
#there may be a better way to do this, but this seems to work for me
while([email protected] && threads->list() != 0) {;}
#kill all threads, write output, read in next batch of urls
my @threads = threads->list();
foreach(@threads) { print("detaching\n"); $_->kill('KILL')->detach(); }#foreach
####### SUBROUTINE CALLED BY THREAD CREATE
sub findCompanyDescription {
my($company_full, $base_url, $url) = @_;
my($descrip, $raw_meta, $raw) = '';
my @company;
$SIG{'KILL'} = sub { alarm(0); threads->exit(); };
eval {
local $SIG{ALRM} = sub { die("alarm\n") }; # NB: \n required
alarm(5);
use Perl::Unsafe::Signals;
UNSAFE_SIGNALS {
while($company) {
my @matches = ($content =~ m!.*<([\w\d]+).*?>\s*about\s+$company[\w\s\-_]*<.*?>(?:<.*?>|\s)*(.*?)</\1.*?>!sig);
MATCH:for(my $ndx=1; $ndx<@matches; $ndx+=2) {
($raw, $descrip) = &filterResult($matches[$ndx], $company_full);
if($descrip) {
$company = undef;
last(MATCH);
}#if
}#for
#reduce the company name and try again
$company = &reduceCompanyName($company);
}#while
alarm(0);
};#unsafe_signals
};#eval
if([email protected]) {
if([email protected] eq "alarm\n" && $DEBUG) { print("\nWebpage Timeout [].\n"); }#if
}#if
if($descrip) { lock($lock); {
@descrip = ($PREV_ID, $company_full, $base_url, $url, 1, $raw, $descrip); }
}#if
小心提及您的平臺? – Dummy00001 2010-08-31 12:15:49
Mac OS X雪豹,有時是Fedora Core 8. – user387049 2010-08-31 15:59:19