Created
March 5, 2022 13:03
-
-
Save ecourtial/0bae7fcfc3cc2f970cb6f9d9d5259810 to your computer and use it in GitHub Desktop.
Fiber exemple 1-3: main
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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