PHP code example of reptily / async-run

1. Go to this page and download the library: Download reptily/async-run 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/ */

    

reptily / async-run example snippets


new AsyncRun(
    int $coresCount = 1,             // Count of running threads
    string $pathTmpDir = '/tmp',     // path to temporary files directory
    ?string $lockFileName = null     // filename for locale
);

Async::run(
    function () {
        sleep(1);
        echo "AAA" . PHP_EOL;
    },
    function () {
        echo "BBB" . PHP_EOL;
    }
)->then(function () {
    echo "CCC" . PHP_EOL;
})->finally(function () {
    echo "DDD" . PHP_EOL;
})->catch(function ($errorText) {
    echo "Error " . $errorText . PHP_EOL;
});

protected function init(): void
{
    $this->arrayTest = [
        [self::FIELD_NAME => 'Bob'],
        [self::FIELD_NAME => 'Mark'],
        [self::FIELD_NAME => 'Ana'],
    ];
}

protected function getError(string $message): void
{
    print_r($message);
}

protected function done(): void
{
    echo "Done in " . $this->getProgressTime() . " sec.\n";
}

(new Example(2))->unlock();

(new Example(2))->run();

protected function workerAfterSpawn(): void
{
    next($this->arrayTest);
}

protected function getWorkerResults(): void
{
    $item = current($this->arrayTest);
    echo $item[self::FIELD_NAME] . "\n";
}

protected function getWorkerDoneCondition(): bool
{
    return current($this->arrayTest) === false;
}