【发布时间】:2012-03-07 04:28:20
【问题描述】:
我希望完成一些类似的事情:
- 用户请求
foo.html - 页面启动 TCPIP 套接字和 HTTP 会话,回显请求头信息
- 页面回显文件的内容
- 页面关闭套接字,用户有文件,大家都很高兴,没有更多的 HTTP 事务进行。
- 函数 FooBar() 被调用...添加数字、发送电子邮件、更新数据库或其他不会阻止向用户输出页面的任务。
在概念上,我的伪 PHP 代码可能如下所示:
<?php
//Send content to the user
echo "Hello world!!";
//This terminates the script,
//I simply want to close the
//HTTP part without terminating
exit();
//That only took a few milliseconds
//Send an email to someone
//containing a sum of numbers
//in the Fibonacci sequence
//This task might take minutes to do.
mail(
"foo@example.com",
"Your sum is ready",
fibonacci(100)
);
但是我没有看到明确的方法来执行此操作,因为 exit() 会终止脚本,并且我没有看到任何可以让我控制 HTTP 套接字的方法。
我见过close a connection early,这是一个有趣的答案,但是我希望在 PHP 5.3 中实现这一点,而不需要输出缓冲和刷新。
【问题讨论】:
-
应该将可能需要几分钟的事情委派给作业队列。让脚本定期检查队列并处理等待的内容。
标签: php apache nonblocking