我已经做了很多关于如何使用PHP创建在后台运行的作业的研究。
<?php
ignore_user_abort(true); // run script in background
set_time_limit(0); // run script forever
// get the size of the output
ob_start();
include_once '';//many includes
//$contentLength = ob_get_length();
// these headers tell the browser to close the connection
// once all content has been transmitted
//header("Content-Length: $contentLength");
header('Connection: close');
// flush all output
ob_end_flush();
ob_flush();
flush();
// close current session
if (session_id()) session_write_close();
//lot of code, fetch query from DB with millions records and do calculations
?>
当我使用标题时(“Content-Length:$ contentLength”);脚本不返回任何内容。当我发表评论时,我会得到数千个使用限制的结果。如果我想要检索所有数百万,服务器在3-4分钟后停止响应
我只需要获取一份巨大的统计报告,并且不希望让用户收到消息 达到最大时间执行。我不想提高时间执行限制,因为限制是未知的。这可能需要长达一个小时。我需要它在后台运行。
答案 0 :(得分:1)
我的方法是以片段形式处理报告,即当时10000行。将每个计算部分保存为文件,然后在输出之前合并所有文件。 您需要通过Ajax调用处理/创建代码段功能。这样,您可以在服务器上创建一个用户体验,但是可以创建多个任务。然后,您还可以显示进度,以便用户知道发生了什么,您还可以检查它是否已经崩溃。