我正在使用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