kzykhys/parallel
最新稳定版本:v0.1.0
Composer 安装命令:
composer require kzykhys/parallel
包简介
Simple multitasking library
README 文档
README
Requirements
- UNIX
- CGI or CLI version of PHP5.4+
- Compiled with --enable-pcntl
Usage
Run multiple tasks asynchronously
<?php use KzykHys\Parallel\Parallel; require __DIR__ . "/vendor/autoload.php"; $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"; } ]); echo "Done\n";
will outputs like this:
task#1 start task#2 start task#1 end task#2 end Done Get an array containing all the results of each job
Notes:
- Internally unix socket is used to receive a value from child process.
- The result must be serializable.
<?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:
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 use KzykHys\Parallel\Parallel; require __DIR__ . "/vendor/autoload.php"; $parallel = new Parallel(); $parallel->each(['a', 'b'], function ($str) { echo "start task with '$str'\n"; sleep(3); echo "finish task with '$str'\n"; });
Get an array containing all the results of each job
Notes:
- Internally unix socket is used to receive a value from child process.
- The result must be serializable.
<?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:
array(3) { [0] => int(2) [1] => int(4) [2] => int(6) } Java like Thread and Runnable
<?php use KzykHys\Thread\Runnable; use KzykHys\Thread\Thread; require __DIR__ . "/vendor/autoload.php"; class Job implements Runnable { public function run() { // do your job } } class AnotherJob extends Thread { public function run() { // do your another job } } $thread1 = new Thread(new Job()); $thread2 = new AnotherJob(); $thread1->start(); $thread2->start(); $thread1->wait(); $thread2->wait();
Installation
Update or create composer.json.
{ "require": { "kzykhys/parallel": "dev-master" } } License
The MIT License
Author
Kazuyuki Hayashi (@kzykhys)
统计信息
- 总下载量: 332.09k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 94
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04