PHP Classes

File: examples/example1.php

Recommend this page to a friend!
  Classes of Jorge Castro   MultiOne   examples/example1.php   Download  
File: examples/example1.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MultiOne
Execute many tasks in parallel using JavaScript
Author: By
Last change:
Date: 3 days ago
Size: 1,569 bytes
 

Contents

Class file image Download
<?php /** @noinspection PhpUnhandledExceptionInspection */

/** @noinspection PhpUnusedParameterInspection */

use Eftec\MultiOne\MultiOne;

include
__DIR__ . '/../vendor/autoload.php';
MultiOne::Factory(
   
1000, // every miliseconds
   
basename(__FILE__), // the url to call
)->setMethods(
    static function(
$numWorkers): array { // the initial call
       
echo "<h1>Example 1 " . MultiOne::VERSION . "</h1>";
        echo
"This example draws a screen then it calls 4 workers every 4 second.<br>";
        echo
"Every worker do some job. Once all workers is done, then it executed the worker-end<br>";
        echo
"starting..";
       
MultiOne::setData(40);
       
// it creates the initial payload for every worker.
       
return array_fill(0, $numWorkers , 'initialpayload');
    },
    static function(
$idWorker, $payload): array { // the worker call
        // it reads the data and reduces to 1.
       
$workPending = MultiOne::getSetDataSafe(static function($old) {
            return
$old - 1;
        });
        if (
$workPending <= 0) {
            return
MultiOne::msgAnswer('end','done','done');
        }
       
usleep(random_int(500, 1500));
        return
MultiOne::msgAnswer('run',$workPending,"Worker: $idWorker, time:" . time() . " pending: " . $workPending);
    },
    static function(
$bodies) { // the worker-end call
       
echo "all worker ended";
    }
)->
setJSMethods(
   
"function(nw,json) {
    console.log(json);
    return json;
    }"
,
   
"function(nw,response) {
    return response;
    }"
)
    ->
setUI()->runAuto();