關於Apache的兩種工作模式prefork和worker

技術小甜發表於2017-11-23

prefork模式

這個多路處理模組(MPM)實現了一個非執行緒型的、預派生的web伺服器,它的工作方式類似於Apache 1.3。它適合於沒有執行緒安全庫,需要避免執行緒相容性問題的系統。它是要求將每個請求相互獨立的情況下最好的MPM,這樣若一個請求出現問題就不會影響到其他請求。

這個MPM具有很強的自我調節能力,只需要很少的配置指令調整。最重要的是將MaxClients設定為一個足夠大的數值以處理潛在的請求高峰,同時又不能太大,以致需要使用的記憶體超出實體記憶體的大小。

worker模式

此多路處理模組(MPM)使網路伺服器支援混合的多執行緒多程式。由於使用執行緒來處理請求,所以可以處理海量請求,而系統資源的開銷小於基於程式的MPM。但是,它也使用了多程式,每個程式又有多個執行緒,以獲得基於程式的MPM的穩定性。

控制這個MPM的最重要的指令是,控制每個子程式允許建立的執行緒數的ThreadsPerChild指令,和控制允許建立的匯流排程數的MaxClients指令。


從定義上看,在PV流量下worker模式明顯優於prefork,但事實上並未如此,下面引用PHP 官方文件


PHP只是一個粘合劑。他將眾多的第三方模組兒庫粘結到一起,然後提供給使用者一個直觀易懂的統一語言介面,讓我們覺得他們就是一個整體。PHP的易用和強大要依賴於一個穩定和強大的執行平臺。他需要作業系統,Web伺服器和第三方模組兒庫融合到一起。他們當中任何一個掛了,PHP都需要通過各種途徑迅速找到問題,並解決。如果我們使用一種沒有完全隔離執行執行緒,沒有完全隔離記憶體空間,並且沒有一個強大的記錄每個需求的容器的Web伺服器來執行PHP時,PHP就會變的很容易出問題。

如果我們想使用一個多執行緒多程式的模組兒,可以關注一下PHP的FastCGI的配置。在FastCGI模式下,PHP執行在自己的記憶體空間裡。

官方原文:

PHP is glue. It is the glue used to build cool web applications by sticking dozens of 3rd-party libraries together and making it all appear as one coherent entity through an intuitive and easy to learn language interface. The flexibility and power of PHP relies on the stability and robustness of the underlying platform. It needs a working OS, a working web server and working 3rd-party libraries to glue together. When any of these stop working PHP needs ways to identify the problems and fix them quickly. When you make the underlying framework more complex by not having completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in, further weaknesses are introduced into PHP`s system.

  • If you want to use a threaded MPM, look at a FastCGI configuration where PHP is running in its own memory space.














本文轉自wks9751CTO部落格,原文連結: http://blog.51cto.com/wks97/1600108,如需轉載請自行聯絡原作者



相關文章