延迟php脚本而不占用连接

时间:2013-05-25 19:21:58

标签: php apache timeout delay

我想问一下是否有人知道我可以延迟一个PHP脚本而不是实际占用连接槽的方法。我并没有完全意识到这一点,但有人告诉我,apache有一个连接限制或脚本运行限制同时我不能完全回忆起来,我的这个脚本需要运行大约1到3个小时它实际上并没有做任何沉重的事情,它实际上只有90%的时间都在睡觉。

2 个答案:

答案 0 :(得分:1)

如果您正在运行脚本而不期望任何响应,则可以使用php "dir/to/php/script.php"在服务器计算机的终端中运行它。

如果脚本的初始化发生在远程,那么您可以让脚本退出,以便脚本继续运行但不保持连接活动。 header('Connection: Close');

示例:

<?php

echo "The server is now doing some complex actions in the background..."; //even maybe a redirect instead
header('Connection: Close');
file_put_contents(file_get_contents("largest_file_in_the_world.txt"),"/tmp/test.txt");

?>

答案 1 :(得分:1)

此外,只是发送连接:关闭标头是不够的,这是连接如何关闭:

ignore_user_abort(true);
header("Connection: close", true);
header("Content-Length: 0", true);
ob_end_flush();
flush();
fastcgi_finish_request();

Source

相关问题