Skip to content

Latest commit

 

History

History
79 lines (58 loc) · 1.51 KB

Queues.md

File metadata and controls

79 lines (58 loc) · 1.51 KB

Queues

A queue is a sequence of callables that will always execute in a FIFO (First In First Out) order.

Each queue is identified by a unique tag, a name.

Queues can overlap while executing, but callables within the queues themselves will never overlap.

Tagging

Two callables that are delayed by 5 and 2 seconds using different tags tag-a and tag-b.

use CatPaw\Queue\Interfaces\QueueInterface;
use function Amp\delay;

function main(QueueInterface $queue):void {
   $start = time();

   $queue->queue("tag-a", static function() {
       delay(5000);
       echo 'a'.PHP_EOL;
   });

   $queue->queue("tag-b", static function() {
       delay(2000);
       echo 'b'.PHP_EOL;
   });

   $queue->consume();

   $finish = time();
   $delta  = $finish - $start;
   echo "$delta seconds have passed.".PHP_EOL;
}

Output

b
a
5 seconds have passed.

Tagging the same identifier twice

Two callables that are delayed by 5 and 2 seconds both using the same tag my-tag.

use CatPaw\Queue\Interfaces\QueueInterface;
use function Amp\delay;

function main(QueueInterface $queue):void {
    $start = time();

    $queue->queue("my-tag", static function() {
        delay(5000);
        echo 'a'.PHP_EOL;
    });

    $queue->queue("my-tag", static function() {
        delay(2000);
        echo 'b'.PHP_EOL;
    });

    $queue->consume();

    $finish = time();
    $delta  = $finish - $start;
    echo "$delta seconds have passed.".PHP_EOL;
}

Output

a
b
7 seconds have passed.