PHP Conference Japan 2024

apache_child_terminate

(PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8)

apache_child_terminate在此請求後終止 apache 行程

說明

apache_child_terminate(): void

apache_child_terminate() 會註冊執行目前 PHP 請求的 Apache 行程,以便在 PHP 程式碼執行完成後終止。它可以用於在執行高記憶體消耗的腳本後終止行程,因為記憶體通常只會在內部釋放,而不會釋放回作業系統。

適用於 Apache 和 FastCGI 網頁伺服器。

參數

此函式沒有參數。

回傳值

不回傳任何值。

注意事項

注意此函式在 Windows 平台上未實作。

參見

  • exit() - 使用狀態碼或訊息終止目前的腳本

新增註記

使用者貢獻的註記 5 則註記

Stephan Ferraro
14 年前
我找到了 Apache 2 的解決方案。然而,這僅適用於無執行緒且僅在 POSIX 相容的作業系統(例如 Linux、OpenSolaris...)上有效。

<?php

// 在請求完成後,
// 透過發送 SIGWINCH POSIX 訊號 (28) 終止 Apache 2 子行程。
function kill_on_exit() {
posix_kill( getmypid(), 28 );
}

register_shutdown_function( 'kill_on_exit' );

?>
jasonlester at nope dot com
1 年前
在 FastCGI SAPI 上,這不會終止行程,它只會讓 fastcgi 處理程式在腳本結束時完全回收 PHP,而不是僅回收請求狀態。這包含 php-cgi。
louis at ewens dot com
16 年前
Apache 子行程很貪婪。如果它們因為需要大量記憶體的 PHP 應用程式而變得臃腫,它們就會保持這種狀態。在該子行程結束之前,記憶體永遠不會釋放回作業系統。

您可以使用 Apache 中的 MaxRequestsPerChild 在一定數量的連線後自動終止所有子行程。或者,您可以在記憶體密集型函式之後使用 apache_child_terminate 終止子行程。

注意:apache_child_terminate 在 Apache 2.0 處理程式中不可用。
daniele_dll at yahoo dot it
16 年前
回覆 sam at liddicott dot com

這並沒那麼簡單!您永遠不應該終止 Apache 行程,因為當 Apache 需要時它會自動釋放!

而且,如果您使用基於 Apache worker 或 thread 的 MPM,您可能會終止整個行程!

結果:不要使用這個函式!
admin at hostultra dot com
16 年前
如果 apache_child_terminate() 函式不存在,這段程式碼將會新增它。

if (!function_exists("apache_child_terminate")){
function apache_child_terminate(){
register_shutdown_function("killonexit");
}

function killonexit(){
@exec("kill ".getmypid());
}
}
To Top