首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP线程池

PHP线程池
EN

Stack Overflow用户
提问于 2014-01-13 18:16:41
回答 2查看 10.9K关注 0票数 4

编辑:来澄清和简化:我正在寻找一种“好的”方法,在堆栈结束时向池提交更多的可堆栈对象(使用来自第一个Stackable的数据来添加第二个堆栈)。我有这样的想法:轮询对象直到结束(低效和丑陋),并传递对Pool对象的引用(我无法使它工作)。基本代码是这样的:https://github.com/krakjoe/pthreads/blob/master/examples/Pooling.php

现在,完整的描述:

我正在用PHP开发一个应用程序,这个应用程序增长得太快了,花费了很多时间。正因为如此,我尝试使用线程池(我知道PHP不是最好的选项,但我不想要,而且此时不能改变语言)来尝试多线程应用程序。

问题是,应用程序有两个阶段,必须按顺序进行,每个阶段都有许多子任务可以同时执行。所以,这就是我脑海中的过程:

  • 阶段1中将有N个子任务,这些子任务将是可堆栈对象。
  • 当子任务i结束时,"main“(创建池、堆栈等)必须得到通知,并使用来自子任务i(一个不同的Stackable对象)的数据执行子任务i的第2阶段。在这个阶段中,阶段1的每个子任务都有M个子任务。

我希望在第1阶段和第2阶段中对线程使用相同的线程池,而从第1阶段到第2阶段的唯一解决方案是轮询每个N个子任务,直到其中一个子任务结束,然后对结束的线程调用第2阶段,然后重复到所有N个子任务结束为止。

我正在使用由Joe编写的p线程源代码中包含的线程池的示例作为基本代码。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-15 07:43:02

你应该从阅读:https://gist.github.com/krakjoe/6437782开始

代码语言:javascript
复制
<?php
/**
* Normal worker
*/
class PooledWorker extends Worker {
    public function run(){}
}


/**
* Don't descend from pthreads, normal objects should be used for pools
*/
class Pool {
    protected $size;
    protected $workers;

    /**
    * Construct a worker pool of the given size
    * @param integer $size
    */  
    public function __construct($size) {
        $this->size = $size;
    }

    /**
    * Start worker threads
    */
    public function start() {
        while (@$worker++ < $this->size) {
            $this->workers[$worker] = new PooledWorker();
            $this->workers[$worker]->start();
        }
        return count($this->workers);
    }

    /**
    * Submit a task to pool
    */
    public function submit(Stackable $task) {
        $this->workers[array_rand($this->workers)]
            ->stack($task);
        return $task;
    }

    /**
    * Shutdown worker threads
    */
    public function shutdown() {
        foreach ($this->workers as $worker)
            $worker->shutdown();
    }
}

class StageTwo extends Stackable {
    /**
    * Construct StageTwo from a part of StageOne data
    * @param int $data
    */
    public function __construct($data) {
        $this->data = $data;
    }

    public function run(){
        printf(
            "Thread %lu got data: %d\n", 
            $this->worker->getThreadId(), $this->data);
    }
}

class StageOne extends Stackable {
    protected $done;

    /**
    * Construct StageOne with suitable storage for data
    * @param StagingData $data
    */
    public function __construct(StagingData $data) {
        $this->data = $data;
    }

    public function run() {
        /* create dummy data array */
        while (@$i++ < 100) {
            $this->data[] = mt_rand(
                20, 1000);
        }
        $this->done = true;
    }
}

/**
* StagingData to hold data from StageOne
*/
class StagingData extends Stackable {
    public function run() {}
}

/* stage and data reference arrays */
$one = [];
$two = [];
$data = [];

$pool = new Pool(8);
$pool->start();

/* construct stage one */
while (count($one) < 10) {
    $staging = new StagingData();
    /* maintain reference counts by storing return value in normal array in local scope */
    $one[] = $pool
        ->submit(new StageOne($staging));
    /* maintain reference counts */
    $data[] = $staging;
}

/* construct stage two */
while (count($one)) {

    /* find completed StageOne objects */
    foreach ($one as $id => $job) {
        /* if done is set, the data from this StageOne can be used */
        if ($job->done) {
            /* use each element of data to create new tasks for StageTwo */
            foreach ($job->data as $chunk) {
                /* submit stage two */
                $two[] = $pool
                    ->submit(new StageTwo($chunk));
            }

            /* no longer required */
            unset($one[$id]);
        }
    }

    /* in the real world, it is unecessary to keep polling the array */
    /* you probably have some work you want to do ... do it :) */
    if (count($one)) {
        /* everyone likes sleep ... */
        usleep(1000000);
    }
}

/* all tasks stacked, the pool can be shutdown */
$pool->shutdown();
?>

将产出:

代码语言:javascript
复制
Thread 140012266239744 got data: 612
Thread 140012275222272 got data: 267
Thread 140012257257216 got data: 971
Thread 140012033140480 got data: 881
Thread 140012257257216 got data: 1000
Thread 140012016355072 got data: 261
Thread 140012257257216 got data: 510
Thread 140012016355072 got data: 148
Thread 140012016355072 got data: 501
Thread 140012257257216 got data: 767
Thread 140012024747776 got data: 504
Thread 140012033140480 got data: 401
Thread 140012275222272 got data: 20
<-- trimmed from 1000 lines -->
Thread 140012041533184 got data: 285
Thread 140012275222272 got data: 811
Thread 140012041533184 got data: 436
Thread 140012257257216 got data: 977
Thread 140012033140480 got data: 830
Thread 140012275222272 got data: 554
Thread 140012024747776 got data: 704
Thread 140012033140480 got data: 50
Thread 140012257257216 got data: 794
Thread 140012024747776 got data: 724
Thread 140012033140480 got data: 624
Thread 140012266239744 got data: 756
Thread 140012284204800 got data: 997
Thread 140012266239744 got data: 708
Thread 140012266239744 got data: 981

因为您想使用一个池,所以您别无选择,只能在创建池的主上下文中创建任务。我可以想象其他的解决方案,但你特别要求这样的解决方案。

根据我所拥有的硬件以及要处理的任务和数据的性质,我很可能有多个小线程池,每个工作线程一个,这将允许StageOne在正在执行它们的工作上下文中创建StageTwo对象,这可能是要考虑的事情。

票数 9
EN

Stack Overflow用户

发布于 2015-06-23 14:44:21

它似乎也很有效:

代码语言:javascript
复制
//Simple example with Collectable (basically Thread meant for Pool) and Pool

<?php

class job extends Collectable {
  public $val;

  public function __construct($val){
    // init some properties
    $this->val = $val;
  }
  public function run(){
    // do some work
    $this->val = $this->val . file_get_contents('http://www.example.com/', null, null, 3, 20);
    $this->setGarbage();
  }
}

// At most 3 threads will work at once
$p = new Pool(3);

$tasks = array(
  new job('0'),
  new job('1'),
  new job('2'),
  new job('3'),
  new job('4'),
  new job('5'),
  new job('6'),
  new job('7'),
  new job('8'),
  new job('9'),
  new job('10'),
);
// Add tasks to pool queue
foreach ($tasks as $task) {
  $p->submit($task);
}

// shutdown will wait for current queue to be completed
$p->shutdown();
// garbage collection check / read results
$p->collect(function($checkingTask){
  echo $checkingTask->val;
  return $checkingTask->isGarbage();
});

?>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21098433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档