Login   Register  
PHP Classes
elePHPant
Icontem

File: examples/demo.sum.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Daniel Martinez  >  Forker PHP  >  examples/demo.sum.php  >  Download  
File: examples/demo.sum.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Forker PHP
Split tasks into multiple forked processes
Author: By
Last change: Changing indentation
Beginning splitting responsabilities
Date: 1 month ago
Size: 1,046 bytes
 

Contents

Class file image Download
<?php
/**************************************************
 * [Forker]
 *
 * Example: Sum of 10 firsts numbers in parallel
 * Usage : php demo.sum.php
 * Storage: Memcache
 **************************************************/
require 'vendor/autoload.php';

use 
Forker\Forker;
use 
Forker\Storage\MemcacheStorage;

$myResult 0;

$myTasks  = array(
    
=> array(1,2),
    
=> array(3,4),
    
=> array(5,6),
    
=> array(7,8),
    
=> array(9,10),
    
=> array(11,12),
);

// a way to keep our data
$storageSystem = new MemcacheStorage;

$numberOfSubTasks 3;

$forker = new Forker($storageSystem$myTasks$numberOfSubTasks);

// My job here is [[1,2] , [3,4]] ,[[5,6],[7,8]]...not precisely in this order
$forker->fork(function($key$myJob$emit) {

    
$total 0;

    foreach(
$myJob as $job) {
        
$total += $job;
    }

    
$emit($key$total);
});

$myResult array_sum($forker->fetch());

$n 12;
$expected = ($n * ($n+1)) / 2;

var_dump($myResult===$expected);
echo 
"Oh my! We could retrieve the sum : {$myResult} \n";