When using WorkerPool, PHP fails to flush output before exiting normally
I tried to use WorkerPool to parallelise some HTTP requests. It seems to work, except for one mysterious bug: when the main/parent PHP process exits, it fails to flush its buffer.
Example file:
<?php
require_once(__DIR__.'/vendor/autoload.php');
echo(htmlspecialchars(json_encode(['Starting...']))."<br>\n");
flush();
$pool = new \QXS\WorkerPool\WorkerPool();
$pool->setWorkerPoolSize(1);
$pool->disableSemaphore();
$pool->create(new \QXS\WorkerPool\ClosureWorker(function (int $value, \QXS\WorkerPool\Semaphore $semaphore, \ArrayObject $storage) : int {
return $value * 2;
}));
foreach ([1, 2, 3] as $input) {
$pool->run($input);
}
$pool->waitForAllWorkers();
foreach ($pool as $return) {
echo(htmlspecialchars(json_encode([$return]))."<br>\n");
flush();
}
echo(htmlspecialchars(json_encode(['Finished!']))."<br>\n");
flush();
echo(htmlspecialchars(json_encode(['Post-flush content.']))."<br>\n");
When visiting the URL of the test file, it outputs the following:
["Starting..."]
[{"pid":30,"data":2}]
[{"pid":30,"data":4}]
[{"pid":30,"data":6}]
["Finished!"]
Note that the content echoed after the last flush() is not present in the output. It seems the PHP process has exited without calling a final flush().
It's being run with PHP Apache. For the record, this is the Dockerfile being used (it has all the required extensions): https://github.com/dxw/wpc/blob/309265e25b4eccdbcf8350d0cf77d5c3c2e4ecba/images/wordpress/Dockerfile
Is there anything that can be done to fix this, other than by calling flush() after all content has been output?
Thanks!
My advice: workerpool was designed for CLI. Try to run the workerpool as cli process by using Popen in Apache-PHP and then send data & collect the results from the cli process.
closing, see above comment