Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php-当线程超过超时时停止线程_Php_Multithreading_Pthreads - Fatal编程技术网

Php-当线程超过超时时停止线程

Php-当线程超过超时时停止线程,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

我正在寻找一种方法来停止/杀死一个线程,因为它需要很长的时间来完成它的任务。我在CentOS 7上,PHP7.2.12。 我尝试使用Threaded::kill()方法,但发现它已不存在。我愿意在线程本身或主线程中停止线程

下面是上下文代码,我尝试并行调用API

$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