婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > 基于PHP-FPM進程池探秘

基于PHP-FPM進程池探秘

熱門標簽:襄陽房產電銷機器人招商 百度地圖標注飯店位置怎么 深圳400電話辦理那家好 清遠陽山400電話號碼如何申請 百度地圖標注名編輯 怎么在高德地圖標注行走軌跡 安徽移動外呼系統 施工地圖標注怎么做 個性化地圖標注在線

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)

最新生成的(模擬)線程登記出現在紅色標示條目位置是因為進程池的并發連接處理能力上限為5,因此它只可能出現在第六條以后的位置。

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)

有意思的是綠色條目代表的(模擬)線程和紅色條目代表的(模擬)線程的登記時間是一樣的,說明兩個(模擬)線程是并發執行的。

2. PHP-FPM配置項pm.max_children = 10,simulated.thread記錄如下:

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)

由于服務端并發連接處理能力上限達到10,因此最新生成的(模擬)線程登記可出現在任何位置。

3. 執行usleep(500)延遲,simulated.thread記錄如下:

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)

可見日志記錄順序與(模擬)線程生成的順序一致。usleep延遲的基本單位是微妙(us, 1 s = 1000000 us)。

從以上的記錄可以看出:

1)這些(模擬)線程是第一次請求執行腳本后就自動生成的,一個(模擬)線程緊接著創建了另一個(模擬)線程;

2)這些(模擬)線程中有的是在同一個子進程空間中產生并運行的;

3)前后相鄰(模擬)線程生成時間間隔很小,幾乎是同時產生,或后一個(模擬)線程在前一個(模擬)線程尚未執行結束并退出之前產生;

4)多個(模擬)線程之間可以并發執行。

所以,上述模擬多線程并發的實現是成功的。PHP-FPM進程池中同一個子進程可先后處理多個連接請求,但同一時間只能處理一個連接請求,未處理連接請求將進入隊列等待處理。換句話,同一個子進程不具有并發處理連接請求的能力。

PHP-FPM Pool配置:它允許定義多個池,每個池可定義不同的配置項。以下只是列舉了我在探秘過程中還關注過的其他部分配置項

1、 listen:The address on which to accept FastCGI requests.它支持TCP Socket和unix socket兩種通訊協議??稍O置listen = [::]:9000。

2、listen.allowed_clients:List of addresses (IPv4/IPv6) of FastCGI clients which are allowed to connect. 該配置項為逗號分隔的列表,如listen.allowed_clients = 127.0.0.1,172.17.0.5。

3、pm:Choose how the process manager will control the number of child processes. 該配置項設置FPM管理進程池的方式,包括static、dynamic、ondemand三種。

4、pm.max_requests:The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries.設置每個子進程處理請求數的上限,對于處理第三方庫中的內存泄漏很有用。

5、pm.status_path:The URI to view the FPM status page.

以上這篇基于PHP-FPM進程池探秘就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php-fpm中max_children的配置
  • centos7上編譯安裝php7以php-fpm方式連接apache
  • php-fpm.conf配置文件中文說明詳解及重要參數說明
  • php-fpm服務啟動腳本的方法
  • php性能分析之php-fpm慢執行日志slow log用法淺析
  • Nginx使用的php-fpm的兩種進程管理方式及優化
  • 一文看懂PHP進程管理器php-fpm

標簽:黑河 延邊 臨夏 阜陽 南昌 欽州 中衛 駐馬店

巨人網絡通訊聲明:本文標題《基于PHP-FPM進程池探秘》,本文關鍵詞  基于,PHP-FPM,進程,池,探秘,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《基于PHP-FPM進程池探秘》相關的同類信息!
  • 本頁收集關于基于PHP-FPM進程池探秘的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 鹤庆县| 怀安县| 浪卡子县| 康乐县| 灵璧县| 仁化县| 来凤县| 苏尼特右旗| 隆尧县| 行唐县| 临邑县| 公安县| 新兴县| 甘洛县| 修水县| 宣城市| 南漳县| 新河县| 平顶山市| 和田县| 鞍山市| 青海省| 中江县| 荆门市| 新龙县| 荆门市| 江北区| 仙桃市| 台山市| 正宁县| 涞水县| 泰宁县| 清远市| 玉龙| 宁强县| 乌鲁木齐市| 巍山| 密云县| 军事| 高平市| 万盛区|