dev-master
9999999-devSimple multitasking library
MIT
The Development Requires
by Kazuyuki Hayashi
v0.1.0
0.1.0.0Simple multitasking library
MIT
The Development Requires
by Kazuyuki Hayashi
Wallogit.com
2017 © Pedro Peláez
Simple multitasking library
``` 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"
}
}
``` 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)
}
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" } }
The MIT License, (*22)
Kazuyuki Hayashi (@kzykhys), (*23)
Simple multitasking library
MIT
Simple multitasking library
MIT