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

參考資料

851 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!

  6. Write My Paper – EssayErudite.com

    Looking for an expert to [b][url=https://essayerudite.com/write-my-paper/]write my paper[/url][/b] for you? You are at the right place.
    Providing superior writing service appears to be our main specialization and passion.
    Our website is the best destination for every English-speaking student who calls for assistance when handling his or her daily academic tasks.

  7. Buy Essay – EssayErudite.com

    Where to Buy Essay Online ? Our experienced writers can boast higher degrees in addition to exceptional writing skills. You now have a great chance to [b][url=https://essayerudite.com/buy-essay/]buy essay[/url][/b] papers online with only a couple of clicks.

  8. We Can Write Your Paper For You – EssayErudite.com

    Can You [url=https://essayerudite.com/write-essay-for-me/]Write My Essay for Me[/url] ? How many times do students worldwide ask this question without finding an answer?
    We’d rather not check the official statistics. At the same time, essays appear to be probably among the most popular academic assignments.
    Students from various education establishments need to complete them constantly. If you are among those lucky ones, feel free to contact our customer service department and opt for an essay writing help

  9. I believe everything posted made a bunch of sense. But,
    think about this, suppose you were to write a awesome headline?
    I mean, I don’t want to tell you how to run your blog, but what if you
    added something that makes people want more? I mean PHP 非同步執行
    – Taien's blog is kinda boring. You could
    look at Yahoo’s home page and see how they create post titles to grab people to open the links.
    You might add a video or a related pic or two to grab readers excited about what you’ve written. In my opinion, it could make your posts a little bit more interesting.

  10. Hello There. I found your weblog the usage of msn. That is a very well written article.

    I’ll make sure to bookmark it and return to read extra of your helpful info.

    Thanks for the post. I will certainly return.

  11. EssayErudite.com is the web’s leading provider of quality and professional academic writing.
    When it comes to essay writing, an in-depth research is a big deal. Our experienced writers are professional in many fields of knowledge so that they can assist you with virtually any academic task. We deliver papers of different types: essays, theses, book reviews, case studies, etc.
    We have thousands of satisfied customers who have already recommended us to their friends. Why not follow their example and place your order today?

  12. Thesis Writing Service – EssayErudite.com

    If you look for a trustworthy [url=https://essayerudite.com/thesis-writing-service/]thesis writing service[/url] and want to benefit from a higher grade, your editors, proofreaders, and instructors are here to lend you a hand.
    Some students afraid of hiring professional writers due to ethical issues. As a result, they fail the course due to various reasons not able to defend their degree.
    You should note that there is nothing wrong with opting for a thesis writing service. EssayErudite is certainly the best place for that.

  13. Yesterday i got some cool info about Omega 3 stuff and now i very wanna get it. But i need a best, some premium omega 3 fish oil. I’ve found this [url=https://www.omega-3.club/premium-fish-oil-carlson.html] carlson omega 3 [/url] . What do you think about this Carlson brand?

  14. Yesterday i read some shocking info about Omega 3 stuff and now i very wanna buy it. But i’m not 100% sure which pluses will i have. I’ve got this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i wanna buy it with best price. Any idea?

  15. Yesterday i read some shocking info about Omega 3 stuff and now i very wanna get it. But i’m not 100% sure which benefits will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i want to buy it with lowest price. Any idea?

  16. Yesterday i got some amazing info about Omega 3 stuff and now i really wanna buy it. But i’m not 100% sure which benefits will i have. I’ve got this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i want to get it with best price. Any idea?

  17. Day ago i read some amazing info about Omega 3 foods and now i very wanna get it. But i’m not 100% sure which benefits will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and now i want to get it with good price. Any idea?

  18. Day ago i read some shocking info about Omega 3 foods and now i really wanna get it. But i’m not absolutely sure which benefits will i have. I’ve got this [url=https://buy-omega-3.com] omega 3 info [/url] , and now i want to get it with good price. Any idea?

  19. Day ago i read some amazing info about Omega 3 foods and now i very wanna get it. But i’m not absolutely sure which benefits will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i wanna buy it with lowest price. Any idea?

  20. Wow, marvelous blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your site is excellent, let
    alone the content!

  21. Day ago i read some amazing info about Omega 3 foods and now i very wanna buy it. But i’m not really sure which pluses will i have. I’ve got this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i wanna buy it with lowest price. Any idea?

  22. скачать бесплатно fifa 15 pc, fifa 15 cracks 3dm v2 а также [url=http://15fifa.ru/skachat-kljuchi-fifa-15/]fifa 15 скачать ключ[/url]

  23. Yesterday i got some interesting info about Omega 3 stuff and now i really wanna buy it. But i’m not 100% sure which benefits will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i want to buy it with good price. Any idea?

  24. Yesterday i read some interesting info about Omega 3 stuff and now i really wanna buy it. But i’m not absolutely sure which pluses will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i want to purchase it with lowest price. Any idea?

  25. First of all I want to say terrific blog! I had a quick question in which I’d like
    to ask if you do not mind. I was interested to find out how you center yourself and clear your thoughts before writing.
    I have had a hard time clearing my mind in getting my ideas out there.
    I truly do take pleasure in writing but it just seems like the first 10 to 15 minutes are generally lost
    simply just trying to figure out how to begin. Any recommendations or tips?
    Thanks!

  26. Day ago i read some shocking info about Omega 3 foods and now i really wanna get it. But i’m not absolutely sure which pluses will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and now i wanna get it with best price. Any idea?

  27. Day ago i read some amazing info about Omega 3 foods and now i really wanna get it. But i’m not 100% sure which pluses will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i want to buy it with lowest price. Any idea?

  28. Great post. I was checking constantly this blog
    and I’m impressed! Very helpful info particularly the last
    section 🙂 I take care of such info a lot. I used
    to be looking for this certain info for a long time.

    Thanks and best of luck.

  29. Yesterday i read some interesting info about Omega 3 stuff and now i very wanna buy it. But i’m not really sure which benefits will i have. I’ve found this [url=https://buy-omega-3.com] omega 3 info [/url] , and currently i want to get it with good price. Any idea?

發表迴響

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

*