Skip to content

Instantly share code, notes, and snippets.

@asanikovich
Created December 12, 2023 11:11
Show Gist options
  • Save asanikovich/37a14cc807d242e8837d770e2d511a6c to your computer and use it in GitHub Desktop.
Save asanikovich/37a14cc807d242e8837d770e2d511a6c to your computer and use it in GitHub Desktop.
fibers.php
<?php
$fiber = new Fiber(function(array $files): void {
foreach ($files as $file) {
unlink(__DIR__.'/'.$file);
Fiber::suspend($file);
}
});
$files = [
'test1.txt',
'test2.txt',
'test3.txt',
];
print "Deleting Files" . PHP_EOL;
$last_file_deleted = $fiber->start($files);
$files_deleted = 1;
$total_files = count($files);
while (!$fiber->isTerminated()) {
$percentage = round($files_deleted / $total_files, 2) * 100;
printf("Deleted %s (%s%% done)." . PHP_EOL, $last_file_deleted, $percentage);
$last_file_deleted = $fiber->resume();
$files_deleted++;
}
print "Completed" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment