danibrutal/forker 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

danibrutal/forker

Composer 安装命令:

composer require danibrutal/forker

包简介

A structured , safe, and easiest way to perform tasks parallely in PHP

README 文档

README

Build Status Latest Stable Version

Synopsis

A structured , safe, and easiest way to perform tasks parallely in PHP.

Code Example

<?php
/**************************************************
 * 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);

$forker->fork(function($city, $url,  $emit) {
  echo "Retrieving weather in $city\n";
  
  $contents = file_get_contents($url);
  $emit($city, $contents);
});

$allCitiesWeather = $forker->fetch();

var_dump($allCitiesWeather);

Motivation

Sometimes we have to work with a huge amount of data. Lately, more and more, and it's just no possible to work sequentially these times.

For example, we have two cooks in a kitchen and a very big carrot. Well, we want our two workers not to be waiting for each other. So why we don't split the carrot in two parts so they can work each one with a part ?

This way both cooks can work together, at the same time, and we will have our dinner soon ! Great huh ?

So, the intention is to make an agile and encapsulated way to split a task in several child subtasks in parallel.

Install

Using composer:

  "require": {
        "danibrutal/forker": "dev-master"
    }

API Reference

You can check the API out here;

Creating your own StorageSystem:

We follow here a TDD aproach so is extremely easy to develop a new system:

1º Create your own storage system following the StorageSystem interface's signature:

/**
   * @param key
   * @param value
   * @return bool
   */
  public function store($key, $value);

  /**
   * @param key
   * @return value | false
   */
  public function get($key);

  /**
   * @return array $tasks
   */
  public function getStoredTasks();

  /**
   * @return bool
   */
  public function cleanUp();

2º Creates a test

<?php
use Forker\Storage\ArrayStorage;
require_once 'BaseStorageTest.php';

class ArrayStorageTest extends BaseStorageTest
{
    protected function getSystemStorage()
    {        
        return new ArrayStorage();        
    }
}

Hard, huh?

3º Then, type phpunit so you can see 3 errors to solve:

There were 4 failures:

1) ArrayStorageTest::testWeCanGetASimpleStoredValue
Failed asserting that null matches expected 'value'.

2) ArrayStorageTest::testWeCanSToreValues
Failed asserting that null is true.

3) ArrayStorageTest::testIcanGetAllMyStoredTasks
Failed asserting that a NULL is not empty.

4) ArrayStorageTest::testWeCanCleanUpAllPreviousTasks
Failed asserting that a NULL is not empty.

4º Just solve the errors. Create your implementation and you are done! Easy and funny :)

Contributors

Please, feel free to colaborate. Fork the project and check [issues][2]. We still have so much work ahead!.

License

MIT

统计信息

  • 总下载量: 58
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固