PHP é tradicionalmente single-threaded com execução sequencial
Imagem retirada do vídeo do canal DifferDev
"Fibers são corrotinas de baixo nível para manipulação de concorrência no PHP."
final class Fiber {
public function __construct(callable $callback) {}
public function start(mixed ...$args): mixed {}
public function resume(mixed $value = null): mixed {}
public function throw(Throwable $exception): mixed {}
public function getReturn(): mixed {}
public function isStarted(): bool {}
public function isSuspended(): bool {}
public function isRunning(): bool {}
public function isTerminated(): bool {}
public static function suspend(mixed $value = null): mixed {}
public static function getCurrent(): ?Fiber {}
}
function inspire(string $name) {
echo "O nome é $name\n";
$resumeValue = Fiber::suspend($name[0]);
echo "$name $resumeValue\n";
return $name;
}
$fiber1 = new Fiber(inspire(...));
$fiber2 = new Fiber(inspire(...));
$suspend1 = $fiber1->start('Rapadura');
$suspend2 = $fiber2->start('PHP');
echo "Fiber1 foi suspensa com $suspend1\n";
echo "Fiber2 foi suspensa com $suspend2\n";
$fiber2->resume('is not dead');
$fiber1->resume('is sweet, but is not soft');
echo "Fiber1 retornou {$fiber1->getReturn()}";
echo "Fiber2 retornou {$fiber2->getReturn()}";
O nome é Rapadura
O nome é PHP
Fiber1 foi suspensa com R
Fiber2 foi suspensa com P
PHP is not dead
Rapadura is sweet, but is not soft
Fiber1 retornou Rapadura
Fiber2 retornou PHP
Obrigado! 🚀