WebApps:处理大量数据时避免超时

时间:2022-10-14 21:44:13

in web applications, how do i handle processing of large amounts of data that usually cause timeouts? eg. processing a db to output a large report? i usually use PHP/Zend Framework/MySQL.

在Web应用程序中,如何处理通常会导致超时的大量数据?例如。处理数据库以输出大型报告?我通常使用PHP / Zend Framework / MySQL。

5 个解决方案

#1


I'd typically execute the task asynchronously - I think you can use something like this in PHP:

我通常异步执行任务 - 我认为你可以在PHP中使用这样的东西:

exec ("/usr/local/bin/php build_report.php")

#2


Asynchronous mechanisms of various sorts. For example, queue up the job in a job table and have an automated task in the background poll the table on a fixed schedule for newly-added jobs, process them, and somehow notify the user it has been done.

各种异步机制。例如,在作业表中排队作业,并在后台使用自动化任务以固定的时间表对新添加的作业进行轮询,处理它们,并以某种方式通知用户它已完成。

In one recent app, a document users must generate is actually requested in one page. This simply translates into the fact that a row is inserted into a job table. An event runs periodically, which performs all the SQL necessary to "compose" the document's data in a second table, and a third step is a PHP page that shows pending/completed documents. In this case, there is no real turnaround requirement, so people aren't actively notified via email or any such mechanism, but it'd be easy to layer that over it all.

在最近的一个应用程序中,用户必须在一个页面中实际生成文档。这简单地转化为将行插入作业表的事实。事件定期运行,执行在第二个表中“组合”文档数据所需的所有SQL,第三步是显示待处理/已完成文档的PHP页面。在这种情况下,没有真正的周转要求,因此人们不会通过电子邮件或任何此类机制主动通知,但很容易将其全部分层。

There's a million ways to skin this cat...

有一百万种皮肤养猫的方法......

#3


While the server side code is processing the data the client will need to poll the server periodically to keep their session alive. If you do not poll the server periodically eventually your HTTP session will expire and cause a timeout error.

当服务器端代码正在处理数据时,客户端将需要定期轮询服务器以使其会话保持活动状态。如果您不定期轮询服务器,最终您的HTTP会话将过期并导致超时错误。

#4


One thing you can do is flush the output buffer, then send some javascript to update a progress bar during your processing. Here's an example:

您可以做的一件事是刷新输出缓冲区,然后在处理过程中发送一些javascript来更新进度条。这是一个例子:

<html>
<body>
<div id="progressBar" style="width: 0px; height: 20px; background: #900; color: #fff;">Progress</div>
</body>
</html>
<?

while(@ob_end_flush());

for ($i = 0; $i < 10; $i++) {

    echo "<script type=\"text/javascript\">var pb = document.getElementById('progressBar'); pb.style.width = '" . ($i * 20) . "px';</script>";
    sleep(1);
}

?>

#5


set_time_limit ( int $seconds ) would allow you to modify the time limit for execution and you can reset it back to normal when you are done executing the scripts which would take a longer time to execute.

set_time_limit(int $ seconds)允许您修改执行的时间限制,并且在完成执行需要较长时间执行的脚本后,可以将其重置为正常。

#1


I'd typically execute the task asynchronously - I think you can use something like this in PHP:

我通常异步执行任务 - 我认为你可以在PHP中使用这样的东西:

exec ("/usr/local/bin/php build_report.php")

#2


Asynchronous mechanisms of various sorts. For example, queue up the job in a job table and have an automated task in the background poll the table on a fixed schedule for newly-added jobs, process them, and somehow notify the user it has been done.

各种异步机制。例如,在作业表中排队作业,并在后台使用自动化任务以固定的时间表对新添加的作业进行轮询,处理它们,并以某种方式通知用户它已完成。

In one recent app, a document users must generate is actually requested in one page. This simply translates into the fact that a row is inserted into a job table. An event runs periodically, which performs all the SQL necessary to "compose" the document's data in a second table, and a third step is a PHP page that shows pending/completed documents. In this case, there is no real turnaround requirement, so people aren't actively notified via email or any such mechanism, but it'd be easy to layer that over it all.

在最近的一个应用程序中,用户必须在一个页面中实际生成文档。这简单地转化为将行插入作业表的事实。事件定期运行,执行在第二个表中“组合”文档数据所需的所有SQL,第三步是显示待处理/已完成文档的PHP页面。在这种情况下,没有真正的周转要求,因此人们不会通过电子邮件或任何此类机制主动通知,但很容易将其全部分层。

There's a million ways to skin this cat...

有一百万种皮肤养猫的方法......

#3


While the server side code is processing the data the client will need to poll the server periodically to keep their session alive. If you do not poll the server periodically eventually your HTTP session will expire and cause a timeout error.

当服务器端代码正在处理数据时,客户端将需要定期轮询服务器以使其会话保持活动状态。如果您不定期轮询服务器,最终您的HTTP会话将过期并导致超时错误。

#4


One thing you can do is flush the output buffer, then send some javascript to update a progress bar during your processing. Here's an example:

您可以做的一件事是刷新输出缓冲区,然后在处理过程中发送一些javascript来更新进度条。这是一个例子:

<html>
<body>
<div id="progressBar" style="width: 0px; height: 20px; background: #900; color: #fff;">Progress</div>
</body>
</html>
<?

while(@ob_end_flush());

for ($i = 0; $i < 10; $i++) {

    echo "<script type=\"text/javascript\">var pb = document.getElementById('progressBar'); pb.style.width = '" . ($i * 20) . "px';</script>";
    sleep(1);
}

?>

#5


set_time_limit ( int $seconds ) would allow you to modify the time limit for execution and you can reset it back to normal when you are done executing the scripts which would take a longer time to execute.

set_time_limit(int $ seconds)允许您修改执行的时间限制,并且在完成执行需要较长时间执行的脚本后,可以将其重置为正常。