PHP 非同步執行

最近與同事討論兩個有趣的參數與函式(ignore_user_abort、fastcgi_finish_request),先留個紀錄

ignore_user_abort (預設 off)

  • 用途:當使用者連線中斷時不影響操作

可透過以下方式操作

  • PHP 碼: ignore_user_abort(true);
  • Nginx 設定:fastcgi_ignore_client_abort off;
  • php.ini:ignore_user_abort=0;

但因階層關係,nginx > PHP,但理論上因為 nginx 已經轉發給 PHP 因此應只要在 PHP 做設定便可生效(尚未測試),猜測其預設 off 原因為一般狀況下使用者斷線不應該再為其服務(例:行動網路瀏覽電商商品頁面),但某些狀況下使用者可能希望有個完整執行(例:交易過程),因此若在有這類多種行為複雜的系統,不應該在 config 中建立起全域設定,而應該透過程式來控制特定需要這類行為的功能。

fastcgi_finish_request

  • 用途:PHP 運作在 FastCGI 下可透過其提早回應瀏覽器
  • PHP 碼:fastcgi_finish_request();
  • 與過去使用 flush 更新進度方式有點像,但本質不一樣

結論

在某些運作需要長時間的操作比如上傳圖片進行其他處理、產生大報表等,可同時使用以上兩個功能,讓其可以快速回應前端又可以完整執行,但正確做法建議還是採用任務排程(例:GearmanResque/Resque-Scheduler等),以避免遇到例外狀況的處理不一。

測試程式

function writeBigFile() { 
    $filepath = "/var/www/html/tmp/" . date("YmdHis") . "-" . rand(1000, 9999) . ".txt"; 
    echo $filepath . "<BR>" . PHP_EOL; $file = fopen($filepath, "a+");
        for ($i = 1; $i <= 10000000; $i++) { 
            fwrite($file, $i . PHP_EOL); 
        } 
}

ignore_user_abort

echo json_encode(array("status" => "ok"), true);
ignore_user_abort(true); // 加與不加 {"status": "ok"} 都會等到 writeBigFile() 執行完才顯示,但該函式可確保使用者中斷後持續把任務完成 
writeBigFile();

fastcgi_finish_request

echo json_encode(array("status" => "ok"), true);
fastcgi_finish_request(); // 該函式會讓 {"status": "ok"} 先回應使用者 
writeBigFile(); 

參考資料

261 comments

  1. Usually I don’t learn post on blogs, but I wish to say that this write-up
    very pressured me to try and do it! Your writing taste has been surprised me.

    Thank you, very nice article.

  2. Hi there just wanted to give you a quick heads up and let you know a few of the pictures aren’t loading properly.
    I’m not sure why but I think its a linking issue.
    I’ve tried it in two different internet browsers and both show
    the same outcome.

  3. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and everything.
    Nevertheless imagine if you added some great photos or videos to give your posts more, “pop”!
    Your content is excellent but with pics and videos, this site could undeniably be one of the
    greatest in its field. Good blog!

  4. It’s really a nice and helpful piece of information. I am happy that you simply shared this useful information with us.
    Please keep us up to date like this. Thank you for
    sharing.

  5. Hiya! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
    My site goes over a lot of the same topics as yours and I feel we could greatly benefit from each other.
    If you are interested feel free to shoot me an email.

    I look forward to hearing from you! Wonderful
    blog by the way!

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

*