Skip to content

Commit

Permalink
修改使用文档,测试修改
Browse files Browse the repository at this point in the history
  • Loading branch information
maozhongyu committed Jun 16, 2021
1 parent c3868e0 commit 976207a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
44 changes: 30 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<h1 align="center"> muti process worker </h1>

<p align="center"> use muti process worker do tasks easily.</p>
<p align="center"> use muti process worker do tasks quickily and easily.</p>

## Introduction

provide child process work space ,you can do task in child work space in onWork function .
also it provide functions to get work content quickily and easily ,you can see `MultiProcessWorker::getWorkContentByIdMode` and `MultiProcessWorker::getWorkContentByOffsetMode`



## Requires

Expand All @@ -19,23 +26,30 @@ $ composer require cr-mao/multi-process-worker

## Usage
```php
use Xpx\MultiProcessWorker\MultiProcessWorker;

use Crmao\MultiProcessWorker\MultiProcessWorker;

$workNum = 4;
$totalTaskNum = 101;
// pcntl模式,
$worker = new MultiProcessWorker(4,101,MultiProcessWorker::modePcntl);
// swooleProcess模式
// $worker = new MultiProcessWorker(4,101,MultiProcessWorker::modeSignleSwooleProcess);
$worker->onWork = function ($startTaskId, $endTaskId, $isLastWorkPage, $workPage, $pid) {
//每个工作空间,如任务数较多,建议分页处理
echo "工作空间编号{$workPage},pid:{$pid}, 负责任务编号{$startTaskId}-{$endTaskId}";
if ($isLastWorkPage) {
echo " 最后一个工作空间,需要跑脚本新增情况考虑,如select * from xxx where id > {$startTaskId}";
}
echo "begin work";
$worker = new MultiProcessWorker($workNum, MultiProcessWorker::modePcntl);
//$worker = new MultiProcessWorker($workNum, MultiProcessWorker::modeSignleSwooleProcess);
$worker->onWork = function ($workPage, $pid) use ($workNum) {
echo PHP_EOL;
echo "工作空间编号:{$workPage},进程id:{$pid}" . PHP_EOL;

//提供便捷函数,快速获得任务内容 ,模式一:id模式
list($beginId, $endId, $isLastWorkPage) = MultiProcessWorker::getWorkContentByIdMode($workPage, 4, 1, 100001);
// select * from xxx where id >={$beginId} AND id <= {$endId},每个进程内可以自行在分页处理, 最后一个工作空间,你也许要考虑数据新增情况
echo "begin mysql id is {$beginId}, end mysql id is {$endId}, 是否是最后一个工作空间:{$isLastWorkPage}";
echo PHP_EOL;

//提供便捷函数,快速获得内容 ,模式二: offset,limit 模式
list($offset, $limit, $isLastWorkPage) = MultiProcessWorker::getWorkContentByOffsetMode($workPage, $workNum, 10001);
echo "offset is {$offset}, limit is {$limit}, 是否是最后一个工作空间:{$isLastWorkPage} ";
echo PHP_EOL;
sleep(3);
};
$worker->start();

```


Expand All @@ -58,3 +72,5 @@ MIT

- https://wiki.swoole.com/#/process/process?id=process

- https://github.com/yidas/php-worker-dispatcher

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"autoload": {
"psr-4": {
"Xpx\\MultiProcessWorker\\": "src"
"Crmao\\MultiProcessWorker\\": "src"
}
}
}
44 changes: 16 additions & 28 deletions test/multi_process_worker_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,28 @@

require_once __DIR__ . "/../vendor/autoload.php";

use Xpx\MultiProcessWorker\MultiProcessWorker;
use Crmao\MultiProcessWorker\MultiProcessWorker;

$workNum = 4;
$totalTaskNum = 101;
$worker = new MultiProcessWorker(100, 10001, MultiProcessWorker::modePcntl);
$worker->onWork = function ($startTaskId, $endTaskId, $isLastWorkPage, $workPage, $pid) {
//每个工作空间,如任务数较多,建议分页处理
echo "工作空间编号{$workPage},pid:{$pid}, 负责任务编号{$startTaskId}-{$endTaskId}";
if ($isLastWorkPage) {
echo " 最后一个工作空间,需要跑脚本新增情况考虑,如select * from xxx where id > {$startTaskId}";
}
echo "begin work";
//$worker = new MultiProcessWorker($workNum, MultiProcessWorker::modePcntl);
$worker = new MultiProcessWorker($workNum, MultiProcessWorker::modeSwooleProcess);
$worker->onWork = function ($workPage, $pid) use ($workNum) {
echo PHP_EOL;
sleep(10);
};
echo "pcntl模式------------start" . PHP_EOL;
$worker->start();

echo "pcntl模式------------end" . PHP_EOL;
echo "工作空间编号:{$workPage},进程id:{$pid}" . PHP_EOL;

sleep(2);
//提供便捷函数,快速获得任务内容,id模式
list($beginId, $endId, $isLastWorkPage) = MultiProcessWorker::getWorkContentByIdMode($workPage, $workNum, 1, 100001);
// select * from xxx where id >={$beginId} AND id <= {$endId},每个进程内可以自行在分页处理, 最后一个工作空间,你也许要考虑数据新增情况
echo "begin mysql id is {$beginId}, end mysql id is {$endId}, 是否是最后一个工作空间:{$isLastWorkPage}";
echo PHP_EOL;

$worker = new MultiProcessWorker(4, 101, MultiProcessWorker::modeSignleSwooleProcess);
$worker->onWork = function ($startTaskId, $endTaskId, $isLastWorkPage, $workPage, $pid) {
//每个工作空间,如任务数较多,建议分页处理
echo "工作空间编号{$workPage},pid:{$pid}, 负责任务编号{$startTaskId}-{$endTaskId}";
if ($isLastWorkPage) {
echo " 最后一个工作空间,需要跑脚本新增情况考虑,如select * from xxx where id > {$startTaskId}";
}
echo " begin work";
sleep(10);
//提供便捷函数,快速获得内容 offset,limit 模式
list($offset, $limit, $isLastWorkPage) = MultiProcessWorker::getWorkContentByOffsetMode($workPage, $workNum, 10001);
echo "offset is {$offset}, limit is {$limit}, 是否是最后一个工作空间:{$isLastWorkPage} ";
// select * from xxx where xxxxx offset {$offset},$limit
echo PHP_EOL;
sleep(3);
};
echo "swooleProcess模式---------start" . PHP_EOL;
$worker->start();
echo "swooleProcess模式---------end" . PHP_EOL;


0 comments on commit 976207a

Please sign in to comment.