发送HTTP响应后继续执行PHP [英] Continue PHP execution after sending HTTP response

查看:144
本文介绍了发送HTTP响应后继续执行PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让PHP 5.2(以apache mod_php运行)向客户端发送完整的HTTP响应,然后再继续执行操作一分钟?

How can I have PHP 5.2 (running as apache mod_php) send a complete HTTP response to the client, and then keep executing operations for one more minute?

长话短说:

我有一个PHP脚本,该脚本必须执行一些较长的数据库请求并发送电子邮件,这需要45到60秒才能运行.我无法控制的应用程序调用了此脚本.我需要该应用程序报告从PHP脚本收到的任何错误消息(主要是无效的参数错误).

I have a PHP script that has to execute a few long database requests and send e-mail, which takes 45 to 60 seconds to run. This script is called by an application that I have no control over. I need the application to report any error messages received from the PHP script (mostly invalid parameter errors).

该应用程序的超时延迟短于45秒(我不知道确切值),因此将PHP脚本的每次执行均注册为错误.因此,我需要PHP尽快将完整的HTTP响应发送到客户端(最好是在验证输入参数之后),然后运行数据库和电子邮件处理.

The application has a timeout delay shorter than 45 seconds (I do not know the exact value) and therefore registers every execution of the PHP script as an error. Therefore, I need PHP to send the complete HTTP response to the client as fast as possible (ideally, as soon as the input parameters have been validated), and then run the database and e-mail processing.

我正在运行mod_php,所以pcntl_fork不可用.我可以通过将要处理的数据保存到数据库并从cron运行实际过程来解决此问题,但是我正在寻找一个更短的解决方案.

I'm running mod_php, so pcntl_fork is not available. I could work my way around this by saving the data to be processed to the database and run the actual process from cron, but I'm looking for a shorter solution.

推荐答案

让处理初始请求的脚本在处理队列中创建一个条目,然后立即返回.然后,创建一个单独的进程(也许通过cron),该进程定期运行队列中所有待处理的作业.

Have the script that handles the initial request create an entry in a processing queue, and then immediately return. Then, create a separate process (via cron maybe) that regularly runs whatever jobs are pending in the queue.

这篇关于发送HTTP响应后继续执行PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆