PHP 支持多進程而不支持多線程;PHP-FPM 在進程池中運行多個子進程并發處理所有連接請求。通過 ps 查看PHP-FPM進程池(pm.start_servers = 2)狀態如下:
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
從列表中可以看出,進程池www中有兩個尚處于空閑狀態的子進程PID 8和 PID 9。注:NLWP指輕量級進程數量,即線程數量。
PHP-FPM(FastCGI Process Manager)是什么?PHP-FPM為PHP-CGI提供進程管理方式,可以有效控制內存和進程,可以平滑重載PHP配置,其master process是常駐內存的。FastCGI是語言無關的、可伸縮架構的CGI開放擴展,其主要行為是將CGI解釋器進程保持在內存中更長時間,不是fork-and-execute,并因此獲得較高的性能。FastCGI支持分布式部署,可以部署在WEB服務器以外的多個主機上。
探秘手段:模擬多線程并發執行
1. 什么是線程:線程有時又稱輕量級進程(Lightweight Process,LWP),通常由線程ID、當前指令指針(PC)、寄存器集合和堆棧組成,是進程中的一個實體,是被系統獨立調度的基本單位;線程自己不擁有系統資源,只擁有一點兒在運行中必不可少的資源,與同屬一個進程的其它線程共享進程所擁有的全部資源。 由于線程之間的相互制約,致使線程在運行中呈現出間斷性。線程也有就緒、阻塞和運行三種基本狀態。由于進程是資源擁有者,創建、撤消與切換開銷過大,在對稱多處理機(SMP)上同時運行多個線程(Threads)才是更合適的選擇。線程的實體包括程序、數據和線程控制塊(Thread Control Block,TCB),TCB包括以下信息:
(1)線程狀態;
(2)當線程不運行時,被保存的現場資源;
(3)一組執行堆棧;
(4)存放每個線程的局部變量主存;
(5)訪問同一個進程中的主存和其它資源。
但使用多個進程會使得應用程序在出現進程池內的進程崩潰或被攻擊的情況下變得更加健壯。
2. 模擬多線程:
<?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...";
探秘匯總:本人通過運行上述腳本后,發現一些可預料但卻不是我曾想到的結果
1. PHP-FPM配置項pm.max_children = 5,simulated.thread記錄如下:
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)