sorry for my twisted english - it is not my native.
I have a trouble: pthreads works, but in some strange way: for example, I cannot do "echo" from a thread.
Some example code from the internet:
<?php
ini_set('display_errors',E_ALL);
class hashThread extends Thread {
private $max = 1;
private $index = 0;
function __construct($max, $index)
{
$this->max = $max;
$this->index = $index;
}
public function run()
{
for ($i=1; $i<=$this->max; $i++)
{
md5($i);
}
error_log("Thread #{$this->index} finished
");
echo "Thread #{$this->index} finished
";
}
}
$thread_count = 8;
$start_time = microtime(true);
for($i=1; $i<=$thread_count; $i++)
{
$thread[$i] = new hashThread(1e6, $i);
$thread[$i]->start(PTHREADS_INHERIT_NONE);
}
echo "Done in: " . round(microtime(true) - $start_time, 2) . " seconds";
?>
Script shows only "Done in: 0.14 seconds" (in webpage), no echoes, But! it is in error log:
$ tail -f /var/log/nginx/error_log | grep Thread 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #2 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #1 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #3 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #6 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #5 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #7 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #4 finished" ... 2014/03/07 08:05:10 [error] 12621#0: *2818394 FastCGI sent in stderr: "PHP message: Thread #8 finished" ...
In fact, I don't use any echo in my threads, I need to aggregate some webservices (each webservice class = thread), wait for the slowest thread, and merge all results to one array that could be taken from outside, but I never worked with threads in PHP or other languages before. I would appreciate if you give me some example. Thank you.
Software:
Gentoo x86
PHP 5.4.23 (nginx configured to use php-fpm)
Nginx 1.4.4
No apache. PHP built threadsafe, pthreads module is enabled.