Skip to content

Instantly share code, notes, and snippets.

@ecourtial
Last active March 5, 2022 15:22
Show Gist options
  • Save ecourtial/7871cd27febcd930dc7998363a14e336 to your computer and use it in GitHub Desktop.
Save ecourtial/7871cd27febcd930dc7998363a14e336 to your computer and use it in GitHub Desktop.
Fiber exemple 2-3: raw execution
<?php
declare(strict_types=1);
class RawExecutionWithoutWaitingForResult
{
public function execute(string $file, int $requestId): void
{
\Fiber::suspend();
// We do not wait for the output, otherwise it would be blocking.
exec('php ' . __DIR__ . "/{$file}.php $requestId > /dev/null 2>&1 &");
// Check if finished, else suspend itself
while (!file_exists("response_{$requestId}.txt")) {
\Fiber::suspend(); // Otherwise we block the thread, anf Fiber becomes useless.
}
echo PHP_EOL . "Request with id #$requestId is finished";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment