我正在使用Thread::Suspend
從遠程模塊啓動線程。部分$subrotine
通話時間超過30秒。Perl線程不會暫停/恢復
my $thr = threads->create(sub {
capture(EXIT_ANY, $^X, $pathToModule, $subroutine, %arguments)
});
return $thr->tid();
我的問題是我無法暫停/恢復創建的線程。下面是代碼執行掛起線程:
use IPC::System::Simple qw (capture $EXITVAL EXIT_ANY);
use threads;
use Thread::Suspend;
use Try::Tiny;
sub suspendThread {
my $msg;
my $threadNumber = shift;
foreach (threads->list()) {
if ($_->tid() == $threadNumber) {
if ($_->is_suspended() == 0) {
try {
# here the execution of the thread is not paused
threads->suspend($_);
} catch {
print "error: " . $! . "\n";
};
$msg = "Process $threadNumber paused";
} else {
$msg = "Process $threadNumber has to be resumed\n";
}
}
}
return $msg;
}
這是從模塊的代碼,我動態地加載:
sub run {
no strict 'refs';
my $funcRef = shift;
my %paramsRef = @_;
print &$funcRef(%paramsRef);
}
run(@ARGV);
我猜測,問題是,sub
傳遞到胎面構造函數調用捕獲(從IPC::System::Simple
模塊)。我也試圖用my $thr = threads->create(capture(EXIT_ANY, $^X, $pathToModule, $subroutine, %arguments));
創建線程任何想法如何解決它。
你想完成什麼?爲什麼不使用例如信號量或其他IPC暫停線程? – Sobrique
當您「不能暫停/恢復創建的線程」時會發生什麼?你看到_error_打印了嗎?什麼是'$ msg'?由於'capture()'至少創建了一個子進程,因此您期望在父進程中掛起一個線程對子進程有什麼影響? – pilcrow
提示:'$ _-> suspend()'看起來比'threads-> suspend($ _)更古怪' – ikegami