runBlocking()

Runs a new coroutine and blocks the current thread until complete all tasks. This static function should not be used from a coroutine.

CoroutineGen::runBlocking(
    function() : \Generator {
        $return = yield file_get_contents('http://www.weather.com.cn/data/cityinfo/101270101.html');
        var_dump($return);
    },
    function() : \Generator {
        yield var_dump("END!");
    },
    function() {
        var_dump("START!");
    },
    function() {
        var_dump("Hello World!");
    }
);

Another

$number = 0;
CoroutineGen::runBlocking(
    function () use (&$number) {
        for ($i = 0; $i < 1000; $i++) {
            $number++;
            yield;
        }
    },
    function () use (&$number) {
        for ($i = 0; $i < 5; $i++) {
            var_dump("A");
            yield;
        }
    },
    function () use (&$number) {
        for ($i = 0; $i < 5; $i++) {
            var_dump("B");
            yield;
        }
    }
);
echo $number;

Output:

string(1) "A"
string(1) "A"
string(1) "B"
string(1) "B"
string(1) "A"
string(1) "B"
string(1) "A"
string(1) "B"
string(1) "A"
string(1) "B"
1000

Last updated