An advanced parallelization library for PHP, enabling efficient multitasking, optimizing resource use, and application responsiveness through multiple CPU threads.
@trowski @kelunik encountering a weird hang when using amphp/parallel and (only when using) ext-uv with JIT, the following example gets stuck with 100% CPU usage on ubuntu 20.04 with the ondrej ppa repos:
test.php:
<?php
use Amp\Parallel\Worker\ContextWorkerPool;
use danog\PixyReproducer\TestTask;
use Revolt\EventLoop;
use function Amp\Future\await;
require 'vendor/autoload.php';
$pool = new ContextWorkerPool(1000);
$states = [];
$futures = [];
for ($x = 0; $x < ($argv[1] ?? 10); $x++) {
echo "Starting $x...\n";
$task = $pool->submit(new TestTask);
$futures []= $task->getFuture();
$channel = $task->getChannel();
EventLoop::queue(function () use ($channel, &$states, $x) {
while ($data = $channel->receive()) {
if (isset($data['ok'])) {
$states[$x] = $data['ok'];
} else {
var_dump($data);
}
}
});
}
echo "Done!\n";
await($futures);
TestTask.php:
<?php
namespace danog\PixyReproducer;
use Amp\Cancellation;
use Amp\Parallel\Worker\Task;
use Amp\Sync\Channel;
use function Amp\delay;
class TestTask implements Task {
/**
* Executed when running the Task in a worker.
*
* @param Channel<TReceive, TSend> $channel Communication channel to parent process.
* @param Cancellation $cancellation Tasks may safely ignore this parameter if they are not cancellable.
*
* @return TResult A specific type can (and should) be declared in implementing classes.
*/
public function run(Channel $channel, Cancellation $cancellation): mixed
{
delay(100);
}
}
@trowski @kelunik encountering a weird hang when using amphp/parallel and (only when using) ext-uv with JIT, the following example gets stuck with 100% CPU usage on ubuntu 20.04 with the ondrej ppa repos:
test.php:
TestTask.php:
Result:
The same example works fine on my arch machine for some reason.