コード例 #1
0
ファイル: demo.timeout.php プロジェクト: danibrutal/forker
 * Example: Retrieving the city-weather using external api
 *          IT Shows how the timeout system works
 *
 * Usage : php examples/demo.timeout.php
 * Storage: Memcache
 **************************************************/
require 'vendor/autoload.php';
use Forker\Forker;
use Forker\Storage\MemcacheStorage;
$allCitiesWeather = "";
$urlApiWeather = "http://api.openweathermap.org/data/2.5/weather?q=%s&mode=xml";
$myTasks = array('madrid' => sprintf($urlApiWeather, 'Madrid'), 'london' => sprintf($urlApiWeather, 'London'), 'new-york' => sprintf($urlApiWeather, 'NewYork'), 'barcelona' => sprintf($urlApiWeather, 'barcelona'), 'lisboa' => sprintf($urlApiWeather, 'lisboa'), 'iasi' => sprintf($urlApiWeather, 'iasi'));
// a way to keep our data
$storageSystem = new MemcacheStorage();
$numberOfSubTasks = 6;
$timeout = 2;
$forker = new Forker($storageSystem, $myTasks, $numberOfSubTasks);
$time_start = microtime(true);
$forker->timeOut($timeout)->fork(function ($city, $url, $emit) {
    if ($city == 'madrid') {
        echo "I'll stay in {$city} for a while..";
        sleep(10);
    }
    echo "Retrieving weather in {$city}\n";
    $contents = file_get_contents($url);
    $emit($city, $contents);
});
$allCitiesWeather = $forker->fetch();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "it took {$time} seconds in parallel \n";
コード例 #2
0
/**************************************************
 * Example: Retrieving the city-weather using external api
 * Usage  : php examples/demo.api.weather.php
 * Storage: File
 **************************************************/
require 'vendor/autoload.php';
use Forker\Forker;
use Forker\Storage\FileStorage;
$allCitiesWeather = "";
$urlApiWeather = "http://api.openweathermap.org/data/2.5/weather?q=%s&mode=xml";
$myTasks = array('madrid' => sprintf($urlApiWeather, 'Madrid'), 'london' => sprintf($urlApiWeather, 'London'), 'new-york' => sprintf($urlApiWeather, 'NewYork'), 'barcelona' => sprintf($urlApiWeather, 'barcelona'), 'lisboa' => sprintf($urlApiWeather, 'lisboa'), 'iasi' => sprintf($urlApiWeather, 'iasi'));
// a way to keep our data
$storageSystem = new FileStorage();
$numberOfSubTasks = 6;
$forker = new Forker($storageSystem, $myTasks, $numberOfSubTasks);
$time_start = microtime(true);
$forker->fork(function ($city, $url, $emit) {
    echo "Retrieving weather in {$city}\n";
    $contents = file_get_contents($url);
    $emit($city, $contents);
});
$allCitiesWeather = $forker->fetch();
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "it took {$time} seconds in paralel \n";
$time_start = microtime(true);
foreach ($myTasks as $city => $url) {
    echo 'Retrieving weather in ' . $city . "\n";
    $allCitiesWeather[] = file_get_contents($url);
}
コード例 #3
0
ファイル: demo.map-reduce.php プロジェクト: danibrutal/forker
$numberOfSubTasks = 3;
$forker = new Forker(new FileStorage(), $myTasks, $numberOfSubTasks);
$path = dirname(__FILE__);
// MAP
$forker->fork(function ($key, $fileName, $emit) use($path) {
    $file_to_get = "{$path}/{$fileName}";
    $content = file_get_contents($file_to_get);
    foreach (getUTF8Words($content) as $word) {
        $emit($word, 1);
    }
});
// REDUCE
$mapped = $forker->fetch();
// We dont set here the number of sub tasks,
// since we don't know the total number
$forker = new Forker(new FileStorage('/tmp/reduced-words'), $mapped);
$forker->fork(function ($word, $counts, $emit) {
    $emit($word, is_array($counts) ? count($counts) : 1);
});
$allWords = $forker->fetch();
arsort($allWords, SORT_NUMERIC);
// First 10 words most used :)
$cont = 10;
foreach ($allWords as $word => $counts) {
    echo $word . " (" . $counts . ")\n";
    if (!--$cont) {
        break;
    }
}
//////////////////////////////////////////////////////////
function getUTF8Words($text)
コード例 #4
0
ファイル: demo.sum.php プロジェクト: danibrutal/forker
<?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(0 => array(1, 2), 1 => array(3, 4), 2 => array(5, 6), 3 => array(7, 8), 4 => array(9, 10), 5 => 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";