从 PHP 脚本运行 PHP 脚本而不阻塞

发布于 2024-08-25 09:45:26 字数 396 浏览 7 评论 0原文

我正在构建一个蜘蛛,它将遍历各个站点并对它们进行数据挖掘。

由于我需要单独获取每个页面,这可能需要很长时间(可能 100 页)。 我已经将 set_time_limit 设置为每页 2 分钟,但 apache 似乎无论如何都会在 5 分钟后杀死脚本。

这通常不是问题,因为这将从 cron 或类似的没有此时间限制的东西运行。不过,我还希望管理员能够通过 HTTP 接口手动启动获取。

apache 在整个持续时间内保持活动状态并不重要,我将使用 AJAX 来触发获取并偶尔使用 AJAX 进行检查。

我的问题是如何从 PHP 脚本中启动提取,而不会在调用它的脚本终止时终止提取。

也许我可以使用 system('script.php &') 但我不确定它是否能解决问题。 还有其他想法吗?

I'm building a spider which will traverse various sites and data mining them.

Since I need to get each page separately this could take a VERY long time (maybe 100 pages).
I've already set the set_time_limit to be 2 minutes per page but it seems like apache will kill the script after 5 minutes no matter.

This isn't usually a problem since this will run from cron or something similar which does not have this time limit. However I would also like the admins to be able to start a fetch manually via a HTTP-interface.

It is not important that apache is kept alive for the full duration, I'm, going to use AJAX to trigger a fetch and check back once in a while with AJAX.

My problem is how to start the fetch from within a PHP-script without the fetch being terminated when the script calling it dies.

Maybe I could use system('script.php &') but I'm not sure it will do the trick.
Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-09-01 09:45:26
    $cmd = "php myscript.php $params > /dev/null 2>/dev/null &";

    # when we call this particular command, the rest of the script 
    # will keep executing, not waiting for a response
    shell_exec($cmd);

其作用是将所有 STDOUT 和 STDERR 发送到 /dev/null,并且您的脚本继续执行。即使“父”脚本在 myscript.php 之前完成,myscript.php 也会完成执行。

    $cmd = "php myscript.php $params > /dev/null 2>/dev/null &";

    # when we call this particular command, the rest of the script 
    # will keep executing, not waiting for a response
    shell_exec($cmd);

What this does is sends all the STDOUT and STDERR to /dev/null, and your script keeps executing. Even if the 'parent' script finishes before myscript.php, myscript.php will finish executing.

听闻余生 2024-09-01 09:45:26

如果你不想使用 exec 你可以使用 php 内置函数!

ignore_user_abort(true);

即使浏览器和服务器之间的连接断开,这也会告诉脚本恢复;)

if you don't want to use exec you can use a php built in function !

ignore_user_abort(true);

this will tell the script to resume even if the connection between the browser and the server is dropped ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文