PHP pthreads是否统计活动线程?

PHP pthreads是否统计活动线程?,php,multithreading,pthreads,Php,Multithreading,Pthreads,我想用pthreads编写一些PHP代码。对于这个pthreads,我需要知道有多少线程处于活动状态。因为下一个系统只能处理固定数量的请求。我可以计算已启动的线程数,但当线程停止时,我不能进行减法 我试图在threads对象上使用count进行计数,但我也不进行减法。如果一个线程已经完成了工作,我如何才能在开始脚本中获取信息?我找到了一种方法来获取关于运行和不运行线程的信息。所以我可以数一数。如果有人有更好的解决方案,我很高兴看到。但首先我想分享我自己的解决方案 #!/usr/bin/php &

我想用pthreads编写一些PHP代码。对于这个pthreads,我需要知道有多少线程处于活动状态。因为下一个系统只能处理固定数量的请求。我可以计算已启动的线程数,但当线程停止时,我不能进行减法


我试图在threads对象上使用count进行计数,但我也不进行减法。如果一个线程已经完成了工作,我如何才能在开始脚本中获取信息?

我找到了一种方法来获取关于运行和不运行线程的信息。所以我可以数一数。如果有人有更好的解决方案,我很高兴看到。但首先我想分享我自己的解决方案

#!/usr/bin/php
<?php
class AsyncOperation extends Thread
{
    public function __construct($threadId)
    {
        $this->threadId = $threadId;
    }

    public function run()
    {
        printf("T %s: Sleeping 3sec\n", $this->threadId);
        sleep(1);
        printf("T %s: Hello World\n", $this->threadId); 
        $this->kill;
    }
}
$a=0; 
$start = microtime(true);
for ($i = 1; $i <= 5; $i++) {
    $test[$i] = new AsyncOperation($i);
    $test[$i]->start();
}
$arg=true;
while($arg){
    $arg=false;
    foreach($test as $key => $object){
//    for ($i = 1; $i <= 5; $i++) {
        $arg2=$object->isRunning();
        if($arg2){
            $arg=$arg2;
        }else{
            //var_dump($key);
            unset ($test[$key]);
        }
//var_dump($key);
if(!$arg){
var_dump($arg);
}
    }
}
var_dump($test);
echo count($test)."\n";
echo "\n".microtime(true) - $start . "\n";
echo "end\n";
#/usr/bin/php

看起来,您需要的是一个线程池。 这本书读起来可能很有趣

池类是工作线程的集合。 通过可调整的线程数,您可以设置具有特定工作线程数的池,以多线程方式执行作业列表

比方说,您只想创建3个线程,处理10个作业

<?php

class MyWork extends Threaded {

    public $name;

    public function __construct($name) {
        echo "Constructing worker $name \n";
        $this->name = $name;
    }

    public function run() {
        echo "Worker $this->name start running\n";
        $strMem = '[mem:' . (memory_get_usage(true) / 1024 / 1024). 'MB]';

        for ($i = 1; $i <= 5; $i++) {
            /*
                random delay is to simulate different time it takes for this worker to process the job.
                in the real work, this is where the job is done
            */
            $delay = rand(1,5); //busy... busy...

            echo "Worker $this->name : $i process in [$delay s] $strMem  \n";
            sleep($delay);
        }
    }
}

class MyWorker extends Worker {
    public function run() {}
}

    ini_set('max_execution_time', 7200);
    ini_set('output_buffering ',0);

    $sJob = "A";//starting job
    $iMaxJob = 10;//max job
    $iMaxThread = 3;//create a pool for 3 worker;

    $pool = new Pool($iMaxThread); 

    //start queing jobs;
    for ($i=1; $i <= $iMaxJob; $i++){
        $pool->submit(new MyWork($sJob++));
    }

    $pool->shutdown();
    $strMem = '[peak:' . (memory_get_peak_usage(true) / 1024 / 1024). 'MB]';

    echo "done. " . $strMem;

?>


不确定这是否有帮助,我对pthreads的了解有限,但我确实使用了我不久前使用的方法,我在数据库中创建了一个带有线程号和布尔值(名为Busy)的表。当线程启动时,它会向表中写入True,而在完成之前,它会写入false。然后,您可以随时引用该表以查看正在使用哪些线程。