swoole进程详解
Posted willem_chen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swoole进程详解相关的知识,希望对你有一定的参考价值。
swoole进程详解
进程详解
进程就是正在运行程序的一个实例
开启进程
Process.php
<?php
$process = new Swoole\\Process('callback_function', true);
$pid = $process->start();
function callback_function(Swoole\\Process $worker)
{
$worker->exec('/www/server/php/71/bin/php', array(__DIR__.'/../http_server.php'));
}
echo $pid.PHP_EOL;
Swoole\\Process::wait();
http_server.php
<?php
$http=new swoole_http_server("0.0.0.0",8812);
$http->set(
[
'enable_static_handler'=>true,
'document_root'=>"/www/wwwroot/test.cc",
]
);
//绑定request请求
$http->on('request',function($request,$response){
//print_r($request->get);
//$response->end("<h1>HTTPserver</h1>");
$response->cookie("willem","chenweiguang",time()+1800);
$response->end("sss".json_encode($request->get));
});
$http->start();
跟踪查看父进程
[root@bogon swoole]# ps aux | grep Process.php
root 4948 0.1 0.3 254648 15344 pts/1 S+ 18:04 0:00 php Process.php
root 4975 0.0 0.0 112812 984 pts/0 S+ 18:04 0:00 grep --color=auto Process.php
[root@bogon swoole]# pstree -p 4948
php(4948)───php(4949)─┬─php(4950)─┬─php(4953)
│ └─php(4954)
├─{php}(4951)
└─{php}(4952)
查看开启的子进程
linux网络状态,一般会用netstat -anp命令查看
树形目录查看进程master,work进程分析
[root@bogon test.cc]# ps aft | grep http_server.php
4949 pts/1 Sl+ 0:00 \\_ /www/server/php/71/bin/php /www/wwwroot/test.cc/swoole/../http_server.php
4950 pts/1 S+ 0:00 \\_ /www/server/php/71/bin/php /www/wwwroot/test.cc/swoole/../http_server.php
4953 pts/1 S+ 0:00 \\_ /www/server/php/71/bin/php /www/wwwroot/test.cc/swoole/../http_server.php
4954 pts/1 S+ 0:00 \\_ /www/server/php/71/bin/php /www/wwwroot/test.cc/swoole/../http_server.php
5715 pts/0 S+ 0:00 \\_ grep --color=auto http_server.php
[root@bogon test.cc]#
exec执行服务端
Process.php
<?php
$process = new Swoole\\Process('callback_function', true);
$pid = $process->start();
function callback_function(Swoole\\Process $worker)
{
$worker->exec('/www/server/php/71/bin/php', array(__DIR__.'/redis.php'));
}
echo $pid.PHP_EOL;
Swoole\\Process::wait();
[root@bogon swoole]# php Process.php
4293
redis.php
<?php
use Swoole\\Coroutine\\Redis;
use function Swoole\\Coroutine\\run;
run(function () {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('123456');
$val = $redis->set('name','process');
print_r($val);
});
swoole进程使用场景
<?php
/*
//传统PHP复制子进程是这样的
$urls=[
'http://baidu.com',
'http://sina.com',
'http://qq.com',
'http://51tx.cn',
];
foreach($urls as $url){
$content[]=file_get_contents($url);
}
*/
# swoole方式
echo "process-start-time:".date("Ymd H:i:s").PHP_EOL;//输出开始时间
$urls=[
'http://baidu.com',
'http://sina.com',
'http://qq.com',
'http://51tx.cn',
];
for($i=0;$i<4;$i++){
# 子进程
$process=new swoole_process(function(swoole_process $worker) use($i,$urls){
//每个子进程执行curl
$content=curlData($urls[$i]);
// echo $content.PHP_EOL;//内容输出到了管道里
# 另一种写入管道方法
$worker->write($content.PHP_EOL);
},true);
$pid=$process->start();
$workers[$pid]=$process;
}
# 打印管道内容
foreach ($workers as $value) {
//打印管道内容用read方法
echo $value->read();
}
/*获取数据的内容
模拟请求URL的内容 ls
*/
function curlData($url){
//正常cul 或 file_get_contents
sleep(1);//延迟一秒
return $url."success".PHP_EOL;
}
# 输出结束时间
echo "process-end-time:".date("Ymd H:i:s").PHP_EOL;
[root@bogon swoole]# php Process.php
process-start-time:20210811 18:22:39
http://baidu.comsuccess
http://sina.comsuccess
http://qq.comsuccess
http://51tx.cnsuccess
process-end-time:20210811 18:22:40
[root@bogon swoole]#
以上是关于swoole进程详解的主要内容,如果未能解决你的问题,请参考以下文章