What is the process
Process is an example of a program is running.
For example, to run a PHP script terminal, a process that is open, there will be a corresponding process ID, that PID.
Check the official documentation
Create a SWOOLE
process
Created in the working directory process.php
<?php
$process = new swoole_process(function(swoole_process $process){
echo 111;
}, true);
$pid = $process->start();
echo $pid . PHP_EOL;
// 当结束的时候,回收结束运行的子进程
swoole_process::wait();
Note: between the process and the process is to communicate through the pipeline. In the example of swoole_process
the time, pass a second argument true
, here feel that the content does not print output to the terminal; if set false
, where you can print to the terminal, do not make the presentation.
The process process.php
starts, the return process ID, here we can see the output pid
[zhengzongqiang@localhost process]$ pwd
/opt/work/htdocs/swoole_mooc/demo/process
[zhengzongqiang@localhost process]$ php process.php
39388
Create a HTTP
child process
In process
open a process http
child process, modify process.php
file
$process = new swoole_process(function(swoole_process $process){
// 这里必须写php的绝对安装路径. /
$process->exec("/opt/soft/php/bin/php", [__DIR__.'/../server/http.php']);
}, true);
$pid = $process->start();
echo $pid . PHP_EOL;
// 当结束的时候,回收结束运行的子进程
swoole_process::wait();
Run the program, it will print the swoole_process
process ID of the process, that is,39658
[zhengzongqiang@localhost process]$ php process.php
39658
Next, we in the new session by viewing port authentication http
child process is turned on
[root@localhost zhengzongqiang]# netstat -anp | grep 8811
tcp 0 0 0.0.0.0:8811 0.0.0.0:* LISTEN 39658/php
We can see that we previously configured http
service port is already being monitored, the parent process ID for 39658
that swoole_process
process to open the http
process.
Process Relations
After the process has been started, you can follow the process of the relationship between the command.
// ps aux | grep process.php
[zhengzongqiang@localhost ~]$ ps aux | grep process.php
zhengzo+ 39657 0.0 0.8 161496 8412 pts/1 S+ 00:54 0:00 /opt/soft/php/bin/php process.php
zhengzo+ 39973 0.0 0.0 112720 980 pts/2 R+ 01:03 0:00 grep --color=auto process.php
You can view the two processes, a process running this command below the representatives, 39657
that is to run process.php
the process ID of the program that 39658
the parent process, that 39657
process has been started the process ID 39658
swool_process process, that 39657
is, php process.php this command process ID.
View the process tree
The relationship between the process can be viewed with the following command
// pstree -p 39657
[zhengzongqiang@localhost ~]$ pstree -p 39657
php(39657)───php(39658)─┬─php(39659)───php(39661)
└─{php}(39660)
Can also correspond to program the following command to view the process
// ps aft | grep http
[zhengzongqiang@localhost ~]$ ps aft | grep http
40193 pts/2 S+ 0:00 \_ grep --color=auto http
39658 pts/1 Sl+ 0:00 \_ /opt/soft/php/bin/php /opt/work/htdocs/swoole_mooc/demo/process/../server/http.php
39659 pts/1 S+ 0:00 \_ /opt/soft/php/bin/php /opt/work/htdocs/swoole_mooc/demo/process/../server/http.php
39661 pts/1 S+ 0:00 \_ /opt/soft/php/bin/php /opt/work/htdocs/swoole_mooc/demo/process/../server/http.php
We can see that the http
process ID 39659
, parent process ID of 39658
the process, namely swoole_process
process, then the parent process is to start swoole_process
the process of the process, namely the process of command file execution, that is 39657
the process. 39659
The process also has a child process, that worker process.
Can be understood as 39658
a process to master the process, 39659
the process for the manager process.
SWOOLE process usage scenarios
Executive multiple URL, need to get the content of this shoe URL address, and then record it to the appropriate library.
PHP is a traditional practice of performing these URL synchronization sequence, this time it will face another problem:
access to every URL address, there will be time consuming, add up, it will lead to the final response time is too long, very unfriendly.
This problem can be introduced swoole process
to address, on-demand open N sub-processes, each child process to perform the appropriate URL, which can greatly shorten the response time.
Prior to the implementation of these processes is a primary URl, introduced swoole process
after the primary process can open multiple sub-processes, so that each sub-process to perform a URl, time will be greatly shortened.
SWOOLE - DEMO
createcurl.php
// 传统实现方式
$urls = [
'http://baidu.com',
'http://sina.com.cn',
'http://qq.com',
'http://baidu.com',
'http://sina.com.cn',
'http://qq.com'
];
foreach($urls as $url){
$content[] = file_get_contents($url);
}
Thus, if each address a request to spend time 1s; it takes a total of 6s.
By swoole_process
realization
<?php
echo "process-start-time:".date('Y-m-d H:i:s').PHP_EOL;
$workers = [];
$urls = [
'http://baidu.com',
'http://sina.com.cn',
'http://qq.com',
'http://baidu.com',
'http://sina.com.cn',
'http://qq.com'
];
for($i = 0; $i < 6; $i++)
{
// 子进程
$process = new swoole_process(function(swoole_process $worker) use ($i, $urls){
$content = curlData($urls[$i]);
echo $content.PHP_EOL;
},true);
$pid = $process->start();
$workers[$pid] = $process;
}
foreach($workers as $worker){
echo $worker->read();
}
function curlData($url)
{
sleep(1);
return $url . 'success' . PHP_EOL ;
}
echo "process-end-time:" . date('Y-m-d H:i:s').PHP_EOL;
Execution of the program found only a 1s
[zhengzongqiang@localhost process]$ php curl.php
process-start-time:2019-06-04 17:46:28
http://baidu.comsuccess
http://sina.com.cnsuccess
http://qq.comsuccess
http://baidu.comsuccess
http://sina.com.cnsuccess
http://qq.comsuccess
process-end-time:2019-06-04 17:46:29
Note: swoole
The constructor content output to a pipe, there is another way
$worker->write($content.PHP_EOL);
Reproduced in: https: //www.jianshu.com/p/97d3a4227202