简体   繁体   English

PHP多线程-如何在所有线程准备就绪后从它们中获取结果?

[英]PHP Multi Threading - How can I obtain the results from all the threads when they are ready?

I am trying to learn multithreading with PHP. 我正在尝试使用PHP学习多线程。 I've installed PHP 7.2.14 with ZTS support, and looked over a lot of examples on the net, and afterwards, tried to create a simple script, to see if I understand the things that I've learned. 我已经安装了具有ZTS支持的PHP 7.2.14 ,并在网上查看了很多示例,然后尝试创建一个简单的脚本,以了解我是否了解所学内容。 The problem is, that it seems, I don't:) 问题是,我似乎没有:)

Here's the script I've made: 这是我制作的脚本:

class Task extends Threaded
{
    private $workToBeDone;

    public $DataHolder;

    public function __construct($i, $z, $DataHolder)
    {
        $this->workToBeDone = array($i, $z);
        $this->DataHolder = $DataHolder;
    }

    public function run()
    {
        $results = 0;

        for ($i=$this->workToBeDone[0]; $i<=$this->workToBeDone[1]; $i++) {
            $results++;
        }

        $this->synchronized(function ($DataHolder) use($results) {
            echo $results . "\n";
            $DataHolder->counter+=$results;
        }, $this->DataHolder);
    }
}

class MyDataHolder {
    public $counter;
}

$DataHolder = new MyDataHolder;
$pool = new Pool(4);
$tasks = array();
for ($i = 0; $i < 15; ++$i) {
    $Task = new Task(1,100, $DataHolder);
    $pool->submit($Task);
}

while ($pool->collect());
$pool->shutdown();

echo "Total: " . $DataHolder->counter;

This script, should create 15 separate tasks, and in each task I would have to iterate a 100 times. 该脚本应该创建15个单独的任务,并且在每个任务中我必须迭代100次。 After each 100 iterations are ready, I would like to store the number of times I've iterated in the MyDataHolder class, to be able to access it later. 在每100次迭代准备好之后,我想将迭代的次数存储在MyDataHolder类中,以便以后可以访问它。

The expected behaviour would be, that when I run this script, I would like to see 100 printed out 15 times on the screen, and in the end, I would like to see Total: 1500 printed out. 预期的行为是,当我运行此脚本时,我希望在屏幕上看到100次打印出来15次,最后,我希望看到Total: 1500打印出来。

Instead of this, 100 is printed out 15 times, but the total value remains empty at the end. 取而代之的是,将15次打印100次,但最后的总值仍为空。

What am I doing wrong? 我究竟做错了什么? How can I collect the data from each of my threads, to use it later on in the program? 我如何从每个线程收集数据,以便以后在程序中使用?

Let me give you similar examples, 让我给你类似的例子,

<?php
/*
Threaded objects (which subsequently includes Volatile objects) are tied to the
context in which they are created. They can be used to fetch data from a thread,
but must be created in the outer most thread in which they are used.
*/
// create Threaded object in the main thread

class DataHolder extends Threaded{
        public $counter;
        public function __construct(){
                $this->counter = 0;
        }
}

class threading extends Thread {
        public $store;
        public $workdone;
        public function __construct(Threaded $store, $i, $z)
        {
                $this->store = $store;
                $this->workdone = array($i, $z);
        }
        public function run()
        {
                /*
                The following array cast is necessary to prevent implicit coerci
on to a
                Volatile object. Without it, accessing $store in the main thread
 after
                this thread has been destroyed would lead to RuntimeException of
:
                "pthreads detected an attempt to connect to an object which has
already
                been destroyed in %s:%d"
                See this StackOverflow post for additional information:
                https://stackoverflow.com/a/44852650/4530326
                */
                $rescount = 0;
                for($i = $this->workdone[0]; $i <= $this->workdone[1]; $i++){
                        $rescount++;
                }
                $this->store['counter'] += $rescount;

        }
}

$store = new DataHolder();
$pool = new Pool(3);
for($i = 0; $i < 15; $i++){
        $task = new threading($store, 1, 100);
        $pool->submit($task);
}

$pool->shutdown();

print_r($store);

?>

This example I tried to modify from examples in github . 我尝试从github中的示例修改此示例。 Sorry I don't fully understand the flow about Threaded objects, and class Thread. 抱歉,我不完全了解有关Threaded对象和Thread类的流程。 Figure yourself about work flow by your own perspective. 用自己的观点来了解工作流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM