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

參考資料

640 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.

  11. Hi would you mind sharing which blog platform you’re working with?

    I’m planning to start my own blog soon but I’m having a hard time selecting
    between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m looking for
    something completely unique. P.S Sorry for being off-topic but I had to ask!

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

  13. Howdy I am so happy I found your website, I really found you by mistake, while I was browsing on Askjeeve for something else, Anyways I am here now and would just like to say
    thanks a lot for a incredible post and a all round interesting blog (I also
    love the theme/design), I don’t have time to look over it all at the
    moment but I have bookmarked it and also added your RSS
    feeds, so when I have time I will be back to read
    much more, Please do keep up the fantastic b.

  14. It is perfect time to make some plans for the long run and it is time to be happy.
    I have learn this put up and if I could I want to recommend you few
    fascinating things or suggestions. Perhaps you could
    write subsequent articles referring to this article.

    I desire to learn more issues approximately it!

  15. Howdy! This article could not be written much better!
    Reading through this article reminds me of my previous roommate!
    He continually kept preaching about this. I am
    going to forward this post to him. Pretty sure he will have a good read.
    Thanks for sharing!

  16. you are truly a good webmaster. The site loading pace is incredible.
    It kind of feels that you’re doing any distinctive trick.
    Also, The contents are masterwork. you have performed a excellent job in this topic!

  17. of course like your web-site however you have to test the spelling on quite a few of your
    posts. A number of them are rife with spelling problems and I find it very troublesome to tell the truth on the other hand I’ll certainly come again again.

  18. An outstanding share! I have just forwarded this onto a coworker who was
    doing a little research on this. And he actually ordered me lunch due to the fact that I stumbled
    upon it for him… lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time
    to talk about this topic here on your website.

  19. It’s appropriate time to make some plans for the future and it is
    time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or tips.

    Perhaps you could write next articles referring to this article.

    I want to read more things about it!

  20. Have you ever thought about adding a little bit
    more than just your articles? I mean, what you say is important and everything.

    Nevertheless just imagine if you added some great photos or videos to give your posts more, “pop”!
    Your content is excellent but with images and video clips, this website could certainly be
    one of the best in its niche. Excellent blog!

  21. Do you mind if I quote a couple of your posts as
    long as I provide credit and sources back to your weblog?

    My blog site is in the very same area of interest as yours and my visitors would certainly
    benefit from a lot of the information you provide here.
    Please let me know if this ok with you. Appreciate it!

  22. Great blog! Do you have any recommendations for aspiring
    writers? I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you advise starting with a free platform like
    Wordpress or go for a paid option? There are so many options
    out there that I’m completely confused .. Any ideas?
    Thanks!

發表迴響

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

*