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. Worker

Example

Code:

<?php

declare(ticks=1);

require_once __DIR__ . '/../../../vendor/autoload.php';

use vennv\vapm\Work;
use vennv\vapm\Worker;
use vennv\vapm\System;

System::time();

$work = new Work();

$work->add(function ($h) : string {
    $number = 0;
    for ($i = 0; $i < 20000; $i++) {
        $number += $i;
        echo $i . PHP_EOL;
    }
    return $h;
}, ["a"]);

$work->add(function () : string {
    // In this will work with thread.
    return 'Hello World! 2';
});

// use 1 thread
$worker = new Worker($work, ['threads' => 2]);

$worker->run(function (array $result, Worker $worker) : void {
    var_dump($result);
    System::timeEnd();
    $worker->done(); // This will void memory leaks
});

Output:

array(2) {
  [0]=>
  string(1) "a"
  [1]=>
  string(14) "Hello World! 2"
}
Time for Console: 1.5306611061096
PreviousWorkerNextStream

Last updated 8 months ago