从 PHP 脚本运行 PHP 脚本而不阻塞
我正在构建一个蜘蛛,它将遍历各个站点并对它们进行数据挖掘。
由于我需要单独获取每个页面,这可能需要很长时间(可能 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其作用是将所有 STDOUT 和 STDERR 发送到 /dev/null,并且您的脚本继续执行。即使“父”脚本在 myscript.php 之前完成,myscript.php 也会完成执行。
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.
如果你不想使用 exec 你可以使用 php 内置函数!
即使浏览器和服务器之间的连接断开,这也会告诉脚本恢复;)
if you don't want to use exec you can use a php built in function !
this will tell the script to resume even if the connection between the browser and the server is dropped ;)