其它语言:English
一个CLI下易用简单的管理进程的库
添加 "pagon/childprocess": "*"
到 composer.json
:
composer.phar install
控制当前进程
declare(ticks = 1) ;
$manager = new ChildProcess();
$manager->on('exit', function () use ($process) {
error_log('exit');
exit;
});
// 做其他事情或等待
在子平行空间运行闭包函数
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->parallel(function () {
sleep(10);
// 做其他事情
});
为了保证主进程不退出来handle事件,可以使用
join
或者while(1)
来保证运行
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->parallel(function () {
// to do something
sleep(10);
error_log('child execute');
}, false);
$child->on('exit', function ($status) {
error_log('child exit ' . $status);
});
// 只运行子进程不等待退出
$child->run()
while(1) { /*to do something */}
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->parallel(function () {
// to do something
sleep(10);
error_log('child execute');
}, false);
$child->on('exit', function ($status) {
error_log('child exit ' . $status);
});
// 等待子进程退出
$child->join();
在子平行空间运行PHP文件
主进程:
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->fork(__DIR__ . '/worker.php', false);
$child->on('exit', function ($status) {
error_log('child exit ' . $status);
});
$child->join();
PHP文件:
$process // The parent process
// 需要做的工作
在子进程运行命令,并且捕获输出
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->spawn('/usr/sbin/netstat', false);
$child->on('stdout', function ($data) {
error_log('receive stdout data: ' . $data);
// to save data or process it
});
$child->on('stderr', function ($data) {
error_log('receive stderr data: ' . $data);
// to save data or process something
});
$child->join();
父子进程可以使用消息来通信
declare(ticks = 1) ;
$manager = new ChildProcess();
$manager->listen();
$child = $manager->parallel(function (Process $process) {
$process->on('message', function ($msg) {
error_log('child revive message: ' . $msg);
});
$process->listen();
$process->send('hello master');
error_log('child execute');
}, false);
$child->on('message', function ($msg) {
error_log('parent receive message: ' . $msg);
});
$child->send('hi child');
$child->join();
当前支持的选项列表:
array(
'cwd' => false, // Current working deirectory
'user' => false, // Startup user
'env' => array(), // Enviroments
'timeout' => 0, // Timeout
'init' => false, // Init callback
'callback' => false // Child startup callback
)
一些用法:
declare(ticks = 1) ;
$manager = new ChildProcess();
$child = $manager->spawn('/usr/sbin/netstat', array(
'timeout' => 60 // Will wait 60 seconds
'callback' => function(){ error_log('netstat start'); }
), false);
$child->on('stdout', function ($data) {
echo $data;
// to save data or process it
});
$child->join();
tick
每个tick都会触发,主要用于监控一些行为来及时反馈到管理器listen
监听消息队列signal
收到任何的信号abort
中断时:SIGINT,SIGTERMfinish
运行结束时(正常退出)exit
管理器进程退出,包含abort
和finish
listen
当前进程实例开始监听队列时触发run
当前进程实例手动运行时init
当前进程实例子进程创建完成时fork
当前进程实例fork时abort
当前进程实例中断时:SIGINT,SIGTERMfinish
当前进程实例结束时(正常退出)exit
当前进程实例退出时,包含abort
和finish