PHP code example of workbunny / process
1. Go to this page and download the library: Download workbunny/process library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
workbunny / process example snippets
// 使用对象方式
$p = new \WorkBunny\Process\Runtime();
$p->child(function(){
var_dump('child');
});
$p = new \WorkBunny\Process\Runtime();
$p->parent(function(){
var_dump('parent'); # 仅输出一次
});
$p = new \WorkBunny\Process\Runtime();
$p->run(function(){
var_dump('child');
},function(){
var_dump('parent');
}, 4); # 1 + 4 进程
$p = new \WorkBunny\Process\Runtime();
$p->wait(function(\WorkBunny\Process\Runtime $parent, int $status){
# 子进程正常退出则会调用该方法,被调用次数是正常退出的子进程数量
},function(\WorkBunny\Process\Runtime $parent, $status){
# 子进程异常退出则会调用该方法,被调用次数是异常的子进程数量
});
$p = new \WorkBunny\Process\Runtime([
'pre_gc' => true,
'priority' => [
0, // 主Runtime优先级为0
-1, // id=1的子Runtime优先级为-1
-2, // id=2的子Runtime优先级为-2
-3 // id=3的子Runtime优先级为-3
]
]);
$p = new \WorkBunny\Process\Runtime();
$p->child(function(\WorkBunny\Process\Runtime $runtime){
var_dump($runtime->getId()); # id !== 0
});
var_dump('parent'); # 打印两次
$p = new \WorkBunny\Process\Runtime();
$p->run(function (\WorkBunny\Process\Runtime $runtime){
},function(\WorkBunny\Process\Runtime $runtime){
}, 4);
var_dump('parent'); # 打印5次
$p = new \WorkBunny\Process\Runtime();
// 创建一个子Runtime
// 假设父RuntimeID === 0,子RuntimeID === 1
// 假设父RuntimePID === 99,子RuntimePID === 100
$id = $p->child(function(\WorkBunny\Process\Runtime $runtime){
$runtime->getId(); // 假设 id === 1
$runtime->getPid(); // 假设 pid === 100
});
if($p->isChild()){
$id === 0; // $id 在子Runtime的上下文中始终为0
posix_getpid() === 100;
}else{
$id === 1;// $id 在当前父Runtime的上下文中为1
posix_getpid() === 99;
}
// 对id === 1的子Runtime进行替换
// 该用法会杀死原id下的子Runtime并新建Runtime替换它
// 该方法并不会改变子Runtime的id,仅改变id对应的pid
$newId = $p->child(function(\WorkBunny\Process\Runtime $runtime){
$runtime->getId(); # id === 1
}, 0, $id);
if($p->isChild()){
$id === $newId === 0;
posix_getpid() !== 100; // 子Runtime PID发生变化,不再是100
// 原PID === 100的子Runtime被kill
}else{
$id === $newId === 1; // $id 没有发生变化
posix_getpid() === 99;
}
$p = new \WorkBunny\Process\Runtime();
$id = $p->child(function(\WorkBunny\Process\Runtime $runtime){
var_dump($runtime->getId()); # id !== 0
var_dump('old-child');
$newP = new \WorkBunny\Process\Runtime();
$newP->child(function(\WorkBunny\Process\Runtime $newP){
var_dump($newP->getId()); # id === 0
var_dump('new-parent');
});
});
# run 方法同理
$p = new \WorkBunny\Process\Runtime();
$p->run(function (){},function(){}, 4);
if($p->getId() === 3){
var_dump('im No. 3'); # 仅id为3的Runtime会生效
}
# fork同理
$p = new \WorkBunny\Process\Runtime();
$p->run(function (){},function(){}, 4);
if($p->isChild()){
var_dump('im child'); # 所有子Runtime都生效
}
# fork同理
$p = new \WorkBunny\Process\Runtime();
$p->run(function (){},function(){}, 4);
if(!$p->isChild()){
var_dump('im parent'); # 父Runtime都生效
}
# 或以注册回调函数来执行
$p->parent(function(\WorkBunny\Process\Runtime $parent){
var_dump('im parent');
});
# fork同理
$p = new \WorkBunny\Process\Runtime();
$p->child(function(\WorkBunny\Process\Runtime $runtime){
var_dump($runtime->getId()); # id !== 0
});
$p->parent(function (\WorkBunny\Process\Runtime $runtime){
var_dump($runtime->getId()); # id === 0
});
$p->run(function (\WorkBunny\Process\Runtime $runtime){
var_dump($runtime->getId()); # id !== 0
},function(\WorkBunny\Process\Runtime $runtime){
var_dump($runtime->getId()); # id === 0
}, 4);
$p = new \WorkBunny\Process\Runtime();
$p->child(function(\WorkBunny\Process\Runtime $runtime){
var_dump('child'); # 生效
$runtime->child(function(){
var_dump('child-child'); # 由于fork作用范围为父Runtime,所以不生效
});
});
$p->parent(function (\WorkBunny\Process\Runtime $runtime){
var_dump('parent'); # 生效
$runtime->child(function(){
var_dump('parent-child'); # 生效
});
});
# run 方法同理
$p = new \WorkBunny\Process\Runtime();
var_dump($p->number(false)); # 仅父Runtime会输出
$p = new \WorkBunny\Process\Runtime();
var_dump($p->getPid()); # 所有Runtime会输出
$p = new \WorkBunny\Process\Runtime();
// $id RuntimeID
// $pid 进程PID
// $status 进程退出状态
$p->wait(function($id, $pid, $status){
# 子Runtime正常退出时
}, function($id, $pid, $status){
# 子Runtime异常退出时
});
$p = new \WorkBunny\Process\Runtime();
// $id RuntimeID
// $pid 进程PID
// $status 进程退出状态
$p->listen(function($id, $pid, $status){
# 子Runtime正常退出时
}, function($id, $pid, $status){
# 子Runtime异常退出时
});
$p = new \WorkBunny\Process\Runtime();
$p->exit(0, 'success');