我如何使用pthread来改进我的网站

时间:2015-12-22 09:06:32

标签: php pthreads multicore

我想使用一些线程进行计算,我的Web服务工作更快。 不要深入计算,它可以正常工作。然而,我的问题是没有使用这个线程它在55秒内采取了一些东西,虽然现在它再次在55秒左右。

所以我想知道我是否以正确的方式使用它? 这是使用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

1 个答案:

答案 0 :(得分:0)

您不应直接调用run()因为它将在主线程中运行代码。

你应该调用start()来创建一个新线程,这个新线程将执行run()

您可以稍后使用join()等待线程完成其工作。

此外,run()不应该返回任何内容。您应该使用共享资源和同步来使完成状态可用于主线程。

示例:

<?php

class WorkerThread extends \Thread
{
    private $eng;
    private $year;
    private $capacity;
    private $list = array();

    public function __construct($eng, $year, $capacity)
    {
        $this->eng = $eng;
        $this->year = $year;
        $this->capacity = $capacity;
    }

    /**
     * Replace with your code
     */
    public function run()
    {
        sleep(2); // thread running this will sleep for 2 seconds
        $this->list = array('chocolate', 'banana', 'pistachio');
    }

    public function getList()
    {
        return $this->list;
    }
}

$worker = new WorkerThread("Lorem", "Ipsum", "FooBar");
var_dump($worker->getList()); // should be empty
$worker->start();

// ... do some other stuff ...

$worker->join(); // main thread waits for WorkerThread to complete
var_dump($worker->getList()); // should have "chocolate", "banana", "pistachio"