PHP code example of thenlabs / task-loop
1. Go to this page and download the library: Download thenlabs/task-loop library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thenlabs / task-loop example snippets
henLabs\TaskLoop\{TaskLoop, CallableTask};
define('DATE_FORMAT', 'Y-m-d H:i:s');
// create a loop instance.
$loop = new TaskLoop();
// adds the task1.
$loop->addTask(function (CallableTask $task) {
static $counter = 10;
echo date(DATE_FORMAT)." Task1: {$counter}\n";
$counter--;
if ($counter <= 0) {
echo date(DATE_FORMAT)." Task1: End\n\n";
// when task1 ends, the loop will be stopped.
$task->getTaskLoop()->stop();
}
});
// adds the task2.
$loop->addTask(function (CallableTask $task) {
static $counter = 5;
echo date(DATE_FORMAT)." Task2: {$counter}\n";
$counter--;
if ($counter <= 0) {
echo date(DATE_FORMAT)." Task2: End\n\n";
// when task2 ends, will be dropped from the loop.
$task->end();
}
});
$delay = 1000000; // value for the usleep function.
$loop->start($delay);
echo 'Good bye!';