How do I close a connection early?

前端 未结 19 1424
情书的邮戳
情书的邮戳 2020-11-22 04:24

I\'m attempting to do an AJAX call (via JQuery) that will initiate a fairly long process. I\'d like the script to simply send a response indicating that the process has star

相关标签:
19条回答
  • You can use Fast-CGI with PHP-FPM to use the fastcgi_end_request() function. In this way, you can continue to do some processing while the response has already been sent to the client.

    • example of how to use fastcgi_finish_request() (Nov 2010)

    You find this in the PHP manual here: FastCGI Process Manager (FPM); But that function specifically is not further documented in the manual. Here the excerpt from the PHP-FPM: PHP FastCGI Process Manager Wiki:


    fastcgi_finish_request()

    Scope: php function

    Category: Optimization

    This feature allows you to speed up implementation of some php queries. Acceleration is possible when there are actions in the process of script execution that do not affect server response. For example, saving the session in memcached can occur after the page has been formed and passed to a web server. fastcgi_finish_request() is a php feature, that stops the response output. Web server immediately starts to transfer response "slowly and sadly" to the client, and php at the same time can do a lot of useful things in the context of a query, such as saving the session, converting the downloaded video, handling all kinds of statistics, etc.

    fastcgi_finish_request() can invoke executing shutdown function.


    Note: fastcgi_finish_request() has a quirk where calls to flush, print, or echo will terminate the script early.

    To avoid that issue, you can call ignore_user_abort(true) right before or after the fastcgi_finish_request call:

    ignore_user_abort(true);
    fastcgi_finish_request();
    
    0 讨论(0)
提交回复
热议问题