管理长时间运行的 php 脚本的最佳方法? [英] Best way to manage long-running php script?
问题描述
我有一个需要很长时间(5-30 分钟)才能完成的 PHP 脚本.以防万一,脚本正在使用 curl 从另一台服务器抓取数据.这就是它需要这么长时间的原因;它必须等待每个页面加载完毕,然后才能处理并移至下一个页面.
I have a PHP script that takes a long time (5-30 minutes) to complete. Just in case it matters, the script is using curl to scrape data from another server. This is the reason it's taking so long; it has to wait for each page to load before processing it and moving to the next.
我希望能够启动脚本并让它一直运行直到完成,这将在数据库表中设置一个标志.
I want to be able to initiate the script and let it be until it's done, which will set a flag in a database table.
我需要知道的是如何能够在脚本完成运行之前结束 http 请求.另外,php 脚本是最好的方法吗?
What I need to know is how to be able to end the http request before the script is finished running. Also, is a php script the best way to do this?
推荐答案
当然可以用 PHP 来完成,但是你不应该把它作为后台任务来做 - 新进程必须从它所在的进程组中分离出来启动.
Certainly it can be done with PHP, however you should NOT do this as a background task - the new process has to be dissocated from the process group where it is initiated.
由于人们一直对这个常见问题给出相同的错误答案,我在这里写了一个更完整的答案:
Since people keep giving the same wrong answer to this FAQ, I've written a fuller answer here:
http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html
来自评论:
简短的版本是 shell_exec('echo/usr/bin/php -q longThing.php | at now');
但是原因为什么",在这里包含的有点长.
The short version is
shell_exec('echo /usr/bin/php -q longThing.php | at now');
but the reasons "why", are a bit long for inclusion here.
这篇关于管理长时间运行的 php 脚本的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!