0
我想使用一些线程来完成我的计算,并且我的web服务工作得更快。 不要深入计算,它工作正常。但是,我的问题是没有使用这个线程它需要55秒左右的时间,但现在又是55秒左右。我如何使用pthread来改进我的网站
所以我想知道我是否以正确的方式使用它? 这是使用pthreads的正确形式吗?
我的PHP版本:5.5.12
class workerThread extends Thread {
public function __construct($eng,$year,$capacity){
$this->eng = $eng;
$this->year = $year;
$this->capacity = $capacity;
}
public function run(){
$counter=0;
$paye1=0;
$paye2=0;
$paye3=0;
foreach($this->eng as $engs){
$data=filter_engs($engs['id'],$this->year,$this->capacity);
if(isset($data['state'])){
if($engs['checkLevel']=='one'){
$paye1++;
$list[$counter]['avgCap']='2304';
$list[$counter]['avg']=ceil(($data['cheked']/2304)*100);
$list[$counter]['fullavg']=ceil(($data['cheked']/$data['allWork'])*100);
}else if($engs['checkLevel']=='two'){
$paye2++;
$list[$counter]['avgCap']='1728';
$list[$counter]['avg']=ceil(($data['cheked']/1728)*100);
$list[$counter]['fullavg']=ceil(($data['cheked']/$data['allWork'])*100);
}else if($engs['checkLevel']=='three'){
$paye3++;
$list[$counter]['avgCap']='1152';
$list[$counter]['avg']=ceil(($data['cheked']/1152)*100);
$list[$counter]['fullavg']=ceil(($data['cheked']/$data['allWork'])*100);
}
$list[$counter]['allWork']=$data['allWork'];
$list[$counter]['wCap']=$data['wCap'];
$list[$counter]['cheked']=$data['cheked'];
$list[$counter]['design']=$data['wCap']-$data['cheked'];
$list[$counter]['countCheked']=$data['countCheked'];
$list[$counter]+=$engs;
$counter++;
}
}
return $list;
}
}
这是我如何使用它:
$worker=new workerThread($eng,$year,$cap);
$list=$worker->run();
$worker->kill();
//and doing some stuff with $list array
太谢谢你了。你能给我一个有用的样本吗? 我怎么能返回我的结果数组到第一个线程再次...? –
很高兴帮助。 我发现http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications带有一个真实世界的例子的答案。它有帮助吗? – Technomad
emmmm,不!不完全...但我明白,谢谢你...我会坚持你的回答关于run(),join()和其他的东西,直到它的工作。 –