Don’t run PHP scripts that take more than ten minutes to run in PHP-FPM.
1. It is recommended that PHP scripts running in PHP-FPM use pclose(popen()) to open a PHP-CLI process without blocking to execute your script.
2. After the execution is completed, insert the message into the database.
3. The front desk performs message notification through AJAX scheduled polling (similar to Zhihu's message notification).
If you must run this kind of time-consuming script in PHP-FPM, it is not impossible.
First you need to configure Nginx to forward this script to a pre-configured independent PHP-FPM dynamic process pool (different PHP-FPM process pools listen to different ports) for processing, so that The execution of the script will not affect other PHP-FPM process pools. Moreover, the dynamic process pool (pm = dynamic) can dynamically fork out more processes to process scripts, at the cost of more memory consumption. In this case, you'd better configure a maximum number of processes (pm.max_children = 20).
If you want PHP-FPM to continue processing business after ending the HTTP request, you can use fastcgi_finish_request()This function provided by PHP-FPM.
Recommended: "PHP Tutorial"
The above is the detailed content of PHP runs for too long and does not respond to other requests.. For more information, please follow other related articles on the PHP Chinese website!