PHP code example of reactphp-x / limiter-concurrent

1. Go to this page and download the library: Download reactphp-x/limiter-concurrent 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/ */

    

reactphp-x / limiter-concurrent example snippets



use ReactphpX\LimiterConcurrent\LimiterConcurrent;
use React\EventLoop\Loop;
use React\Promise\Deferred;
use function React\Async\delay;


// 10 request per second but not in order
$limiterConcurrent = new LimiterConcurrent(10, 1000);


$start = microtime(true);
for ($i = 0; $i < 100; $i++) {
    $limiterConcurrent->concurrent(function () use ($i) {
        $deferred = new Deferred();
        Loop::addTimer(0.1 * random_int(1, 10), function () use ($deferred, $i) {
            $deferred->resolve($i);
        });
        return $deferred->promise();
    })->then(function ($i) use ($start) {
        $end = microtime(true);
        echo "then $i " . ($end - $start) . "\n";
    });
}

delay(12);

// 10 request per second in order
$limiterConcurrent = new LimiterConcurrent(10, 1000, true);

$start = microtime(true);
for ($i = 0; $i < 100; $i++) {
    $limiterConcurrent->concurrent(function () use ($i) {
        $deferred = new Deferred();
        Loop::addTimer(0.1 * random_int(1, 10), function () use ($deferred, $i) {
            $deferred->resolve($i);
        });
        return $deferred->promise();
    })->then(function ($i) use ($start) {
        $end = microtime(true);
        echo "queue then $i " . ($end - $start) . "\n";
    });
}