Php-当线程超过超时时停止线程
我正在寻找一种方法来停止/杀死一个线程,因为它需要很长的时间来完成它的任务。我在CentOS 7上,PHP7.2.12。 我尝试使用Threaded::kill()方法,但发现它已不存在。我愿意在线程本身或主线程中停止线程 下面是上下文代码,我尝试并行调用APIPhp-当线程超过超时时停止线程,php,multithreading,pthreads,Php,Multithreading,Pthreads,我正在寻找一种方法来停止/杀死一个线程,因为它需要很长的时间来完成它的任务。我在CentOS 7上,PHP7.2.12。 我尝试使用Threaded::kill()方法,但发现它已不存在。我愿意在线程本身或主线程中停止线程 下面是上下文代码,我尝试并行调用API $finalReport = array(); $timeout = 5; $sources = array(new PullReport(new MakeDataSource("")), new PullReport(new Mod
$finalReport = array();
$timeout = 5;
$sources = array(new PullReport(new MakeDataSource("")), new PullReport(new ModelDataSource("")));
$startTime = time();
$isPending = true;
foreach ($sources as $source) {
//echo $source->get();
$source->start();
$source->join();
}
foreach ($sources as $source) {
while (!$source->getDataSource()->hasData()) {
if (time() - $startTime > $timeout) {
$source->kill();
break;
}
echo "no data";
usleep(100);
}
}
foreach ($sources as $source) {
$finalReport[] = $source->data;
}
print_r($finalReport);
编辑:或者,除了直接终止线程,还有哪些替代方法?使用。
但是,考虑使用类似队列的RabBMQ.< /P>线程代码在哪里,使用什么线程机制?PECL Pthreads已经中断,但通常在调用自己的kill或stop方法时会设置一个信号量
public function run(){while($this->isRunning){/*dowork*/}}
然后您的stop()
方法只设置$this->isRunning=false代码>