pthreads with phalcon 3.3 - PHP 致命错误

pthreads with phalcon 3.3 - PHP Fatal error

我是 Phalcon 和 Pthreads 的新手。

我的环境如下:

$php-v PHP 7.2.2 (cli)(内置:2018 年 2 月 19 日 10:04:19)(ZTS DEBUG) 版权所有 (c) 1997-2018 PHP 集团 Zend Engine v3.2.0,版权所有 (c) 1998-2018 Zend Technologies 使用 Zend OPcache v7.2.2,版权所有 (c) 1999-2018,Zend Technologies

$php-m | grep 线程 pthreads

phpinfo() 表示如下:

PHP 版本 7.0.25-0ubuntu0.16.04.1

禁用线程安全

尽管使用 --enable-maintainer-zts \ --with-tsrm-pthreads 编译

我遵循了这些说明: https://blog.programster.org/ubuntu16-04-compile-php-7-2-with-pthreads

我在同一个文件夹中有两段代码:

class Task extends Threaded
{
    private $value;

    public function __construct(int $i){
        $this->value = $i;
    }

    public function run(){
        $s=0;

        $rand = rand(1, 100);
        $sleep = rand(1, 500);

        for ($i=0; $i<$rand; $i++){
            $s++;
            time_nanosleep (0, $sleep);
        }

        echo "Task: {$this->value}\n";
    }
}

# Create a pool of $n threads
$n = 16;
$pool = new Pool($n);

for ($i = 0; $i < 1000; ++$i){
    $pool->submit(new Task($i));
}

while ($pool->collect());

$pool->shutdown();

此代码运行完美。

我的另一段代码是从 phalcon 实例化的。

<?php
class MultiapiPool
{
    private $providers;
    private $dependencies;
    private $input;

    public function __construct($p, $d, $i){
        $this->providers = $p;
        $this->dependencies = $d;
        $this->input = $i;
    }

    private function getProviders(){
        return $this->providers;
    }

    private function getDependencies(){
        return $this->dependencies;
    }

    private function getInput(){
        return $this->input;
    }

    public function run(){

        $providers = $this->getProviders();
        $pool = new Pool(count($providers));

        return array(
            'input' =>$this->getInput(),
            'dependencies'=>$this->getDependencies(),
            'providers'=>$providers);
    }
}

这个 class 抛出一个错误:

PHP 致命错误:未捕获错误:Class 'Pool' 在 /var/www/html/tutorial/app/libraries/MultiapiPool 中找不到。php:29

违规行是:$pool = new Pool(count($providers));

我的问题是:

  1. new Pool() 如何在一个文件中起作用而在另一个文件中不起作用? 工作文件中没有特殊包含。

  2. 禁用线程安全

尽管使用 --enable-maintainer-zts \ --with-tsrm-pthreads 编译

你的 class 正在扩展线程:

class Task extends Threaded

你的 class 不工作没有扩展线程:

class MultiapiPool

尝试扩展 MultiapiPool 以使用 Threaded,看看是否能解决问题:

class MultiapiPool extends Threaded