I am new to Phalcon and Pthreads.
My environment is as follows:
$ php -v PHP 7.2.2 (cli) (built: Feb 19 2018 10:04:19) ( ZTS DEBUG ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.2, Copyright (c) 1999-2018, by Zend Technologies
$ php -m | grep pthreads pthreads
phpinfo() says the following:
PHP Version 7.0.25-0ubuntu0.16.04.1
Thread Safety disabled
Despite compilation with --enable-maintainer-zts \ --with-tsrm-pthreads
I followed these instructions: https://blog.programster.org/ubuntu16-04-compile-php-7-2-with-pthreads
I have two pieces of code in the same folder:
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();
This code runs perfectly.
My other piece of code is instantiated from 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);
}
}
This class throws an error:
PHP Fatal error: Uncaught Error: Class 'Pool' not found in /var/www/html/tutorial/app/libraries/MultiapiPool.php:29
The offending line is: $pool = new Pool(count($providers));
My questions are:
How new Pool() works in one file but not in another? There are no special includes in the working file.
Thread Safety disabled
Despite compilation with --enable-maintainer-zts \ --with-tsrm-pthreads
Your class that works is extending Threaded:
class Task extends Threaded
Your class that doesn't work is not extending Threaded:
class MultiapiPool
Try to extend MultiapiPool to use Threaded and see if that fixes things:
class MultiapiPool extends Threaded