symplely/coroutine

View on GitHub
examples/future.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/**
 * @see https://github.com/amphp/parallel/blob/master/examples/worker-pool.php
 */
include 'vendor/autoload.php';

use function Async\Worker\add_future;
// A variable to store our fetched results
$results = [];
// We can first define tasks and then run them
$tasks = ['http://php.net', 'https://amphp.org', 'https://github.com'];

function repeat()
{
    $counter = 0;
    while (true) {
        $counter++;
        if ($counter == 200) {
            $counter = 0;
            \printf(".");
        }
        yield;
    }
}

function enqueue($index, $task)
{
    echo 'started ' . $index . \EOL;
    // return to caller, let other tasks start, otherwise block after
    $result = yield add_future(function () use ($task) {
        return \file_get_contents($task);
    });

    $tid = yield \current_task();
    \printf("\nRead from %d, task %d: %d bytes\n", $index, $tid, \strlen($result));
    return $result;
};

// Event loop for parallel tasks
function main()
{
    global $results, $tasks;

    $coroutinesId = [];
    foreach ($tasks as $index => $parameters) {
        $coroutinesId[] = yield \away(\enqueue($index, $parameters));
    }

    try {
        // will throw exception and stop/kill progress printout '.' after 1 seconds
        yield \wait_for(\repeat(), 1);
    } catch (\Async\TimeoutError $e) {
        $results = yield \gather($coroutinesId);
    }
};

\coroutine_run(\main());

echo "\nResult array keys:\n";
echo \var_export(\array_keys($results), true);