2017 © Pedro Peláez
 

library parallel

Simple multitasking library

image

kzykhys/parallel

Simple multitasking library

  • Tuesday, February 4, 2014
  • by kzykhys
  • Repository
  • 6 Watchers
  • 92 Stars
  • 221,213 Installations
  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 12 Forks
  • 3 Open issues
  • 2 Versions
  • 3 % Grown

The README.md

Parallel.php - Simple multitasking library

Latest Unstable Version Build Status Coverage Status, (*1)

Requirements

  • UNIX
  • CGI or CLI version of PHP5.4+
  • Compiled with --enable-pcntl

Usage

Run multiple tasks asynchronously

``` php <?php, (*2)

use KzykHys\Parallel\Parallel;, (*3)

require DIR . "/vendor/autoload.php";, (*4)

$parallel = new Parallel(); $parallel->run([ function () { echo "task#1 start\n"; sleep(2); echo "task#1 end\n"; }, function () { echo "task#2 start\n"; sleep(3); echo "task#2 end\n"; } ]);, (*5)

echo "Done\n";, (*6)


will outputs like this:

task#1 start task#2 start task#1 end task#2 end Done, (*7)


#### Get an array containing all the results of each job ##### Notes: 1. Internally unix socket is used to receive a value from child process. 2. The result must be serializable. ``` php <?php use KzykHys\Parallel\Parallel; require __DIR__ . "/vendor/autoload.php"; $parallel = new Parallel(); $values = $parallel->values([ function () { return 'item'; }, 'foo' => function () { // return value must be serializable return new \DateTime('2013-01-01'); } ]); var_dump($values);

will output like this:, (*8)

array(2) {
  [0] =>
  string(4) "item"
  'foo' =>
  class DateTime#6 (3) {
    public $date =>
    string(19) "2013-01-01 00:00:00"
    public $timezone_type =>
    int(3)
    public $timezone =>
    string(10) "Asia/Tokyo"
  }
}

Process multiple values asynchronously

``` php <?php, (*9)

use KzykHys\Parallel\Parallel;, (*10)

require DIR . "/vendor/autoload.php";, (*11)

$parallel = new Parallel(); $parallel->each(['a', 'b'], function ($str) { echo "start task with '$str'\n"; sleep(3); echo "finish task with '$str'\n"; });, (*12)


#### Get an array containing all the results of each job ##### Notes: 1. Internally unix socket is used to receive a value from child process. 2. The result must be serializable. ``` php <?php use KzykHys\Parallel\Parallel; require __DIR__ . "/vendor/autoload.php"; $parallel = new Parallel(); $values = $parallel->map([1, 2, 3], function ($value) { return $value * 2; }); var_dump($values);

will output like this:, (*13)

array(3) {
  [0] =>
  int(2)
  [1] =>
  int(4)
  [2] =>
  int(6)
}

Java like Thread and Runnable

``` php <?php, (*14)

use KzykHys\Thread\Runnable; use KzykHys\Thread\Thread;, (*15)

require DIR . "/vendor/autoload.php";, (*16)

class Job implements Runnable { public function run() { // do your job } }, (*17)

class AnotherJob extends Thread { public function run() { // do your another job } }, (*18)

$thread1 = new Thread(new Job()); $thread2 = new AnotherJob();, (*19)

$thread1->start(); $thread2->start();, (*20)

$thread1->wait(); $thread2->wait();, (*21)




Installation ------------ Update or create composer.json. ``` json { "require": { "kzykhys/parallel": "dev-master" } }

License

The MIT License, (*22)

Author

Kazuyuki Hayashi (@kzykhys), (*23)

The Versions

04/02 2014

dev-master

9999999-dev

Simple multitasking library

  Sources   Download

MIT

The Development Requires

by Kazuyuki Hayashi

04/02 2014

v0.1.0

0.1.0.0

Simple multitasking library

  Sources   Download

MIT

The Development Requires

by Kazuyuki Hayashi