Quickstart
Minimal working example
The example below creates three workers, waits for the group, and then inspects the result of each worker.
<?php
declare(strict_types=1);
use FastForward\Fork\Manager\ForkManager;
use FastForward\Fork\Worker\WorkerInterface;
$manager = new ForkManager();
$group = $manager->fork(
static function (WorkerInterface $worker): int {
echo sprintf("worker %d started\n", $worker->getPid());
usleep(150_000);
echo sprintf("worker %d finished\n", $worker->getPid());
return 0;
},
3,
);
$group->wait();
foreach ($group as $worker) {
printf(
"pid=%d exit=%s signal=%s\n",
$worker->getPid(),
var_export($worker->getExitCode(), true),
$worker->getTerminationSignal()?->name ?? 'none',
);
}
What happens here
ForkManageris created in the master process.fork()spawns three workers that all run the same callback.- The callback receives the current
WorkerInterfaceinstance. WorkerGroup::wait()blocks until all workers in that group finish.- Each worker can then be inspected for exit code, signal, stdout, and stderr.
Expected behavior
- Each worker gets its own PID.
- Each worker runs independently.
getExitCode()returns0when the callback returns0.getTerminationSignal()staysnullwhen the worker exits normally.getOutput()contains the buffered worker output captured by the parent.
Next steps
After the quickstart, continue with: