案例1
<?php
$process = new \Swoole\Process(function(\Swoole\Process $pro){
$pro->write("xixiahjh");
},true);
$pid = $process->start();
echo $pid.PHP_EOL;
案例2
<?php
$process = new \Swoole\Process(function(\Swoole\Process $pro){
$pro->exec("/www/usr/bin/php/php8.0.16/bin/php",[__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'server'.DIRECTORY_SEPARATOR.'httpserver.php']);
},true);
$pid = $process->start();
echo $pid.PHP_EOL;
\Swoole\Process::wait();
[root@996a3fae2cbc process]# php8016 demo.php
141
[root@996a3fae2cbc /]# netstat -anp|grep 9501
tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 141/php
[root@996a3fae2cbc /]# netstat -anp|grep 9501
tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 141/php
tcp 0 0 172.17.0.2:9501 172.17.0.1:35640 ESTABLISHED 141/php
tcp 0 0 172.17.0.2:9501 172.17.0.1:35634 ESTABLISHED 141/php
[root@996a3fae2cbc /]# netstat -anp|grep 9501
tcp 0 0 0.0.0.0:9501 0.0.0.0:* LISTEN 141/php
tcp 0 0 172.17.0.2:9501 172.17.0.1:35640 ESTABLISHED 141/php
tcp 0 0 172.17.0.2:9501 172.17.0.1:35634 ESTABLISHED 141/php
[root@996a3fae2cbc /]#
[root@996a3fae2cbc /]# ps aux|grep demo.php
root 140 0.0 0.1 61556 13708 pts/4 S+ 16:10 0:00 php8016 demo.php
root 172 0.0 0.0 10700 2308 pts/3 S+ 16:13 0:00 grep --color=auto demo.php
[root@996a3fae2cbc /]# pstree -p 141
php(141)─┬─php(142)─┬─php(143)
│ ├─php(144)
│ ├─php(148)
│ ├─php(154)
│ ├─php(155)
│ ├─php(156)
│ ├─php(157)
│ └─php(158)
├─{php}(145)
├─{php}(146)
├─{php}(147)
├─{php}(149)
├─{php}(150)
├─{php}(151)
├─{php}(152)
└─{php}(153)
[root@996a3fae2cbc /]#
[root@996a3fae2cbc /]# ps aft|grep httpserver.php
141 pts/4 Sl+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
142 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
143 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
144 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
148 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
154 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
155 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
156 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
157 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
158 pts/4 S+ 0:00 \_ /www/usr/bin/php/php8.0.16/bin/php /www/wwwroot/swoole_demo/process/../server/httpserver.php
207 pts/3 S+ 0:00 \_ grep --color=auto httpserver.php
案例3
<?php
echo date("Y-m-d H:i:s").PHP_EOL;
$workers = [];
$urls = [
"http://sina.com.com/",
"http://baidu.com",
"http://qq.com",
"http://baidu.com/?search=qq",
"http://baidu.com/?search=qq1",
"http://baidu.com/?search=qq21",
];
$c = count($urls);
for($i = 0;$i<$c;$i++){
$process = new \Swoole\Process(function(\Swoole\Process $pro) use($i,$urls){
$connect = curl_test($urls[$i]);
echo $connect.PHP_EOL;
},true);
$pid = $process->start();
$workers[$pid] = $process;
}
foreach ($workers as $pro){
echo $pro->read();
}
function curl_test($url){
sleep(1);
return $url."SUCCESS".PHP_EOL;
}
echo date("Y-m-d H:i:s").PHP_EOL;
[root@996a3fae2cbc process]# php8016 curl.php
2022-03-03 14:33:57
http://sina.com.com/SUCCESS
http://baidu.comSUCCESS
http://qq.comSUCCESS
http://baidu.com/?search=qqSUCCESS
http://baidu.com/?search=qq1SUCCESS
http://baidu.com/?search=qq21SUCCESS
2022-03-03 14:33:58
[root@996a3fae2cbc process]#
|