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(); 

參考資料

220 comments

  1. Hi there, I found your web site by the use of Google even as searching for
    a similar topic, your website got here up, it appears to be like great.

    I have bookmarked it in my google bookmarks.

    Hi there, simply become aware of your weblog through Google, and found that it is really informative.
    I’m going to be careful for brussels. I’ll appreciate in the
    event you continue this in future. Many folks can be benefited from your
    writing. Cheers!

  2. I’m really loving the theme/design of your website. Do you ever run into any web browser compatibility issues?
    A couple of my blog readers have complained about my blog not operating correctly in Explorer
    but looks great in Safari. Do you have any recommendations to
    help fix this problem?

  3. Spot on with this write-up, I actually think this web site needs
    a great deal more attention. I’ll probably be returning to read more, thanks
    for the info!

  4. This is the right website for anyone who would like to understand this topic.

    You understand a whole lot its almost hard to
    argue with you (not that I actually would want to…HaHa).

    You certainly put a fresh spin on a topic that’s been discussed
    for a long time. Excellent stuff, just excellent!

  5. I’m truly enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more
    often. Did you hire out a designer to create
    your theme? Outstanding work!

  6. It’s the best time to make a few plans for the longer term and it is time
    to be happy. I have learn this publish and if I could I desire to suggest you some fascinating
    things or suggestions. Perhaps you can write next articles relating to this article.
    I desire to learn even more things approximately it!

  7. Please let me know if you’re looking for a author for your blog.

    You have some really great posts and I think I would be
    a good asset. If you ever want to take some of the load off,
    I’d love to write some material for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Cheers!

  8. This is very interesting, You’re a very skilled blogger.
    I have joined your feed and look forward to seeking more of
    your fantastic post. Also, I’ve shared your site in my social
    networks!

  9. My spouse and I absolutely love your blog and find most of your post’s to be
    exactly what I’m looking for. Does one offer
    guest writers to write content available for you? I wouldn’t mind creating a post or elaborating on most of the subjects you write regarding here.
    Again, awesome web site!

  10. I have been surfing online greater than 3 hours today, yet I never discovered any attention-grabbing article like yours.
    It’s pretty value sufficient for me. Personally, if all web owners and bloggers made good content material as you did, the web will likely be much more helpful than ever
    before.

發表迴響

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

*