【问题标题】:PHP pthreads count active threads?PHP pthreads 计算活动线程?
【发布时间】:2014-07-07 13:19:55
【问题描述】:

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

我尝试对线程对象进行计数,但我也没有减去。如果一个线程完成了他的工作,我该如何管理,我会在我的启动脚本中获得信息?

【问题讨论】:

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

标签: php multithreading pthreads


【解决方案1】:

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

#!/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";

【讨论】:

    【解决方案2】:

    看来,你需要的是一个线程池。 这可能会很有趣。

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

    假设您只想创建 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;
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 2018-09-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多