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

參考資料

331 comments

  1. I know this if off topic but I’m looking into starting my own weblog and
    was wondering what all is required to get set up?
    I’m assuming having a blog like yours would cost
    a pretty penny? I’m not very internet savvy so I’m not 100% sure.
    Any suggestions or advice would be greatly appreciated.

    Kudos

  2. Greetings from Carolina! I’m bored to tears at work so I decided to browse your
    blog on my iphone during lunch break. I really like the information you present here and can’t wait to take a look when I get home.

    I’m amazed at how quick your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyways, excellent blog!

  3. Everyone loves what you guys are usually up too. This type of clever work
    and coverage! Keep up the wonderful works guys I’ve incorporated you guys to my blogroll.

  4. I’m really enjoying the design and layout of your site.

    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did
    you hire out a designer to create your theme? Great work!

  5. Today, while I was at work, my sister stole my iphone and tested to see if it can survive a forty foot drop,
    just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views.
    I know this is completely off topic but I had to share it with
    someone!

  6. Hey there just wanted to give you a quick heads up.

    The text in your article seem to be running off the screen in Ie.

    I’m not sure if this is a formatting issue or something to
    do with internet browser compatibility but I figured I’d post to let you
    know. The design and style look great though! Hope you
    get the problem fixed soon. Cheers

  7. It’s genuinely very complicated in this busy life to listen news on Television, so
    I simply use internet for that purpose, and obtain the hottest news.

  8. Heya i’m for the first time here. I found this board and I find It really useful
    & it helped me out a lot. I hope to give something back
    and help others like you helped me.

  9. Heya are using WordPress for your blog platform? I’m new
    to the blog world but I’m trying to get started and set up my own. Do you
    need any coding expertise to make your own blog?

    Any help would be really appreciated!

  10. Greetings I am so thrilled I found your site, I really found
    you by error, while I was looking on Askjeeve for something else,
    Anyways I am here now and would just like to say cheers for a fantastic post and a all round
    exciting blog (I also love the theme/design), I don’t have time to go through it all at the moment but
    I have saved it and also added in your RSS feeds,
    so when I have time I will be back to read more, Please do keep up the superb work.

  11. Hey there! I know this is kind of off topic but I was wondering which blog platform are you using for this site?

    I’m getting tired of WordPress because I’ve had problems with hackers and I’m
    looking at alternatives for another platform.

    I would be great if you could point me in the direction of a good platform.

  12. I’m really enjoying the theme/design of your web site.
    Do you ever run into any web browser compatibility issues?
    A small number of my blog readers have complained
    about my website not operating correctly in Explorer but
    looks great in Safari. Do you have any ideas to
    help fix this issue?

  13. Hey there! I could have sworn I’ve been to this site before
    but after reading through some of the post I realized it’s new to me.
    Nonetheless, I’m definitely glad I found it and I’ll
    be bookmarking and checking back often!

  14. Cool blog! Is your theme custom made or did you download it from somewhere?

    A theme like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your theme. Many thanks

  15. After going over a few of the blog posts on your web page, I truly
    like your technique of writing a blog. I bookmarked it to my bookmark
    webpage list and will be checking back soon. Take a look at my website as well and tell me what
    you think.

  16. I like the helpful info you supply on your articles.
    I will bookmark your blog and check again here frequently.
    I’m moderately sure I’ll be informed plenty of new stuff right right
    here! Best of luck for the following!

  17. Hey are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and set up my
    own. Do you need any html coding knowledge to make your
    own blog? Any help would be greatly appreciated!

  18. Hello! I simply would like to give you a big thumbs up for your great information you have here on this post.
    I am coming back to your website for more soon.

  19. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored material stylish. nonetheless, you command get
    bought an edginess over that you wish be delivering the following.
    unwell unquestionably come further formerly again as exactly the same nearly very often inside case
    you shield this hike.

  20. I do trust all of the ideas you have offered on your post.
    They are very convincing and will definitely work.
    Nonetheless, the posts are too brief for newbies. May just you please extend them
    a little from next time? Thank you for the post.

  21. I’m no longer positive where you are getting your information, but good topic.
    I needs to spend a while finding out much more or figuring out more.
    Thanks for great information I used to be looking for this
    information for my mission.

  22. It’s actually a nice and useful piece of information. I’m satisfied that you
    shared this helpful information with us. Please stay us informed like
    this. Thank you for sharing.

  23. Appreciating the persistence you put into your site and in depth
    information you present. It’s awesome to come across a blog every
    once in a while that isn’t the same outdated rehashed material.
    Great read! I’ve saved your site and I’m including your RSS feeds to my Google account.

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

發表迴響

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

*