Skip to content

Instantly share code, notes, and snippets.

@ecourtial
Created March 5, 2022 13:03
Show Gist options
  • Save ecourtial/0bae7fcfc3cc2f970cb6f9d9d5259810 to your computer and use it in GitHub Desktop.
Save ecourtial/0bae7fcfc3cc2f970cb6f9d9d5259810 to your computer and use it in GitHub Desktop.
Fiber exemple 1-3: main
<?php
declare(strict_types=1);
require_once 'require/raw-exec.php';
$requestIds = [1,2,3];
$asyncAction = new RawExecutionWithoutWaitingForResult();
// Cleanup on startup (see block-curl-action.php)
foreach ($requestIds as $id) {
@unlink("response_{$id}.txt");
}
// Create the fibers
$fibers = [];
foreach ($requestIds as $id) {
$fiber = new \Fiber($asyncAction->execute(...));
$fiber->start('blocking-curl-action', $id);
$fibers[] = $fiber;
}
// Run the fibers
while($fibers !== []) {
foreach ($fibers as $key => $fiber) {
if($fiber->isSuspended()) {
$fiber->resume();
}
if ($fiber->isTerminated()) {
unset($fibers[$key]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment