root@d856fd02d2fe:~# ps aux -L USER PID LWP %CPU NLWP %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 1 0.0 1 0.0 4504 692 ? Ss 13:10 0:00 /bin/sh /usr/local/php/bin/php-fpm start root 7 7 0.0 1 0.4 176076 19304 ? Ss 13:10 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) www-data 8 8 0.0 1 0.2 176076 8132 ? S 13:10 0:00 php-fpm: pool www www-data 9 9 0.0 1 0.2 176076 8132 ? S 13:10 0:00 php-fpm: pool www root 10 10 0.0 1 0.0 18376 3476 ? Ss 14:11 0:00 bash root 66 66 0.0 1 0.0 34420 2920 ? R+ 15:13 0:00 ps aux -L
<?php
/**
* PHP 只支持多进程不支持多线程。
*
* PHP-FPM 在进程池中运行多个子进程并发处理所有连接,
* 同一个子进程可先后处理多个连接请求,但同一时间
* 只能处理一个连接请求,未处理连接请求将进入队列等待处理
*
*/
class SimulatedThread
{
//模拟线程
private $thread;
//主机名
private $host = 'tcp://172.17.0.5';
//端口号
private $port = 80;
public function __construct()
{
//采用当前时间给线程编号
$this->thread = microtime(true);
}
/**
* 通过socket发送一个新的HTTP连接请求到本机,
* 此时当前模拟线程既是服务端又是模拟客户端
*
* 当前(程序)子进程sleep(1)后会延迟1s才继续执行,但其持有的连接是继续有效的,
* 不能处理新的连接请求,故这种做法会降低进程池处理并发连接请求的能力,
* 类似延迟处理还有time_nanosleep()、time_sleep_until()、usleep()。
* 而且sleep(1)这种做法并不安全,nginx依然可能出现如下错误:
* “epoll_wait() reported that client prematurely closed connection,
* so upstream connection is closed too while connecting to upstream”
*
* @return void
*/
public function simulate()
{
$run = $_GET['run'] ?? 0;
if ($run++ < 9) {//最多模拟10个线程
$fp = fsockopen($this->host, $this->port);
fputs($fp, "GET {$_SERVER['PHP_SELF']}?run={$run}\r\n\r\n");
sleep(1);//usleep(500)
fclose($fp);
}
$this->log();
}
/**
* 日志记录当前模拟线程运行时间
*
* @return void
*/
private function log()
{
$fp = fopen('simulated.thread', 'a');
fputs($fp, "Log thread {$this->thread} at " . microtime(true) . "(s)\r\n");
fclose($fp);
}
}
$thread = new SimulatedThread();
$thread->simulate();
echo "Started to simulate threads...";
Log thread 1508054181.4236 at 1508054182.4244(s) Log thread 1508054181.4248 at 1508054182.4254(s) Log thread 1508054181.426 at 1508054182.428(s) Log thread 1508054181.6095 at 1508054182.6104(s) Log thread 1508054182.4254 at 1508054183.4262(s) Log thread 1508054183.4272 at 1508054183.4272(s) Log thread 1508054182.4269 at 1508054183.4275(s) Log thread 1508054182.4289 at 1508054183.43(s) Log thread 1508054182.6085 at 1508054183.6091(s) Log thread 1508054182.611 at 1508054183.6118(s)
Log thread 1508058075.042 at 1508058076.0428(s) Log thread 1508058075.0432 at 1508058076.0439(s) Log thread 1508058075.0443 at 1508058076.045(s) Log thread 1508058075.6623 at 1508058076.6634(s) Log thread 1508058076.0447 at 1508058077.0455(s) Log thread 1508058076.046 at 1508058077.0466(s) Log thread 1508058077.0465 at 1508058077.0466(s) Log thread 1508058076.0469 at 1508058077.0474(s) Log thread 1508058076.6647 at 1508058077.6659(s) Log thread 1508058076.6664 at 1508058077.6671(s)
Log thread 1508061169.7956 at 1508061170.7963(s) Log thread 1508061169.7966 at 1508061170.7976(s) Log thread 1508061169.7978 at 1508061170.7988(s) Log thread 1508061170.2896 at 1508061171.2901(s) Log thread 1508061170.7972 at 1508061171.7978(s) Log thread 1508061171.7984 at 1508061171.7985(s) Log thread 1508061170.7982 at 1508061171.7986(s) Log thread 1508061170.7994 at 1508061171.8(s) Log thread 1508061171.2907 at 1508061172.2912(s) Log thread 1508061171.2912 at 1508061172.2915(s)
Log thread 1508059270.3195 at 1508059270.3206(s) Log thread 1508059270.3208 at 1508059270.3219(s) Log thread 1508059270.322 at 1508059270.323(s) Log thread 1508059270.323 at 1508059270.324(s) Log thread 1508059270.3244 at 1508059270.3261(s) Log thread 1508059270.3256 at 1508059270.3271(s) Log thread 1508059270.3275 at 1508059270.3286(s) Log thread 1508059270.3288 at 1508059270.3299(s) Log thread 1508059270.3299 at 1508059270.331(s) Log thread 1508059270.3313 at 1508059270.3314(s)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有