Vapm
  • About
    • Why is it called unblocking?
    • Next update?
    • Credit
    • Info
  • System
    • runEventLoop()
    • runSingleEventLoop()
    • setTimeout()
    • setInterval()
    • fetch()
    • read()
    • time() & timeEnd()
  • Asynchronous
    • getId()
    • await()
  • Promise
    • c()
    • getId()
    • getFiber()
    • isJustGetResult()
    • getTimeOut()
    • getTimeStart()
    • getTimeEnd()
    • setTimeEnd()
    • canDrop()
    • getStatus()
    • getResult()
    • getReturn()
    • getCallback()
    • resolve() & reject()
    • then() & catch() & finally()
    • useCallbacks()
    • all()
    • allSettled()
    • any()
    • race()
    • Some common methods
  • CoroutineGen
    • runBlocking()
    • runNonBlocking()
    • Deferred
    • Deferred Await All
    • repeat() & delay()
    • Channel
      • Example about Channel
    • AwaitGroup
      • Example
    • Mutex
      • Example
    • Goroutine vs CoroutineGen
      • Concurrency
  • Thread
    • How do I create a thread?
    • How do I share data between threads?
    • Advanced Thread
  • Worker
    • Example
  • Stream
    • read()
    • write()
    • append()
    • delete()
    • create()
    • overWrite()
  • FAQ
    • Why System::runEventLoop() and System::runSingleEventLoop() ?
    • Do you want concurrency?
  • PocketMine-PMMP
Powered by GitBook
On this page
  1. CoroutineGen

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
PreviousCoroutineGenNextrunNonBlocking()

Last updated 9 months ago