我有一个 PHP 脚本需要很长时间(5-30 分钟)才能完成。为了以防万一,该脚本正在使用curl从另一台服务器上抓取数据。这就是它花了这么长时间的原因;它必须等待每个页面加载,然后才能处理它并移动到下一个页面。
我希望能够启动脚本并让它一直运行直到完成,这将在数据库表中设置一个标志。
我需要知道的是如何能够在脚本运行完成之前结束http请求。另外,php 脚本是执行此操作的最佳方法吗?
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.
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?
发布评论
评论(16)
更新 +12 年 - 安全说明
虽然这仍然是调用长时间运行的代码的好方法,但限制甚至禁用 Web 服务器中的 PHP 启动其他代码的能力对于安全性是有好处的。可执行文件。由于这将日志运行的行为与启动它的行为分离,因此在许多情况下,使用守护程序或 cron 作业可能更合适。
原始答案
当然可以使用 PHP 来完成,但是您不应该将其作为后台任务来执行 - 新进程必须与其启动的进程组分离。
由于人们不断对此常见问题解答给出相同的错误答案,因此我在这里写了一个更完整的答案:
http://symcbean.blogspot.com/2010/02/php-and-long-running-processes.html
来自评论:
Update +12 years - Security Note
While this is still a good way to invoke a long running bit of code, it is good for security to limit or even disable the ability of PHP in the webserver to launch other executables. And since this decouples the behaviour of the log running thing from that which started it, in many cases it may be more appropriate to use a daemon or a cron job.
Original Answer
Certainly it can be done with PHP, however you should NOT do this as a background task - the new process has to be dissociated 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
From the comments:
快速但肮脏的方法是使用 php.ini 中的
ignore_user_abort
函数。这基本上是说:不要关心用户做什么,运行这个脚本直到完成。如果它是一个面向公众的站点,这有点危险(因为如果启动 20 次,您最终可能会同时运行 20++ 版本的脚本)。“干净”的方式(至少恕我直言)是在您想要启动进程并每小时(左右)运行一个 cronjob 来检查是否设置了该标志时设置一个标志(例如在数据库中)。如果设置了,则长时间运行的脚本将启动,如果未设置,则不会发生任何事情。
The quick and dirty way would be to use the
ignore_user_abort
function in php. This basically says: Don't care what the user does, run this script until it is finished. This is somewhat dangerous if it is a public facing site (because it is possible, that you end up having 20++ versions of the script running at the same time if it is initiated 20 times).The "clean" way (at least IMHO) is to set a flag (in the db for example) when you want to initiate the process and run a cronjob every hour (or so) to check if that flag is set. If it IS set, the long running script starts, if it is NOT set, nothin happens.
您可以使用 exec 或 system 启动后台作业,然后在其中执行工作。
此外,还有比您正在使用的更好的方法来抓取网络。您可以使用线程方法(多个线程一次执行一页),或使用事件循环的方法(一个线程一次执行多个页面)。我个人使用 Perl 的方法是使用 AnyEvent::HTTP。
ETA: symcbean 解释了如何分离此处正确进行后台进程。
You could use exec or system to start a background job, and then do the work in that.
Also, there are better approaches to scraping the web that the one you're using. You could use a threaded approach (multiple threads doing one page at a time), or one using an eventloop (one thread doing multiple pages at at time). My personal approach using Perl would be using AnyEvent::HTTP.
ETA: symcbean explained how to detach the background process properly here.
是的,您可以用 PHP 来完成。但除了 PHP 之外,使用队列管理器也是明智之举。策略如下:
将大任务分解为小任务。在您的情况下,每个任务都可以加载一个页面。
将每个小任务发送到队列。
在某处运行您的队列工作人员。
使用此策略具有以下优点:
对于长时间运行的任务,它能够在运行中途发生致命问题时进行恢复 - 无需从头开始。
如果您的任务不必按顺序运行,您可以运行多个工作程序来同时运行任务。
如果
您有多种选择(这只是其中一些):
Yes, you can do it in PHP. But in addition to PHP it would be wise to use a Queue Manager. Here's the strategy:
Break up your large task into smaller tasks. In your case, each task could be loading a single page.
Send each small task to the queue.
Run your queue workers somewhere.
Using this strategy has the following advantages:
For long running tasks it has the ability to recover in case a fatal problem occurs in the middle of the run -- no need to start from the beginning.
If your tasks do not have to be run sequentially, you can run multiple workers to run tasks simultaneously.
You have a variety of options (this is just a few):
不,PHP 不是最好的解决方案。
我不确定 Ruby 或 Perl,但使用 Python,您可以将页面抓取器重写为多线程,并且它的运行速度可能至少快 20 倍。编写多线程应用程序可能有些挑战,但我编写的第一个 Python 应用程序是多线程页面抓取器。您可以使用 shell 执行函数之一从 PHP 页面中简单地调用 Python 脚本。
No, PHP is not the best solution.
I'm not sure about Ruby or Perl, but with Python you could rewrite your page scraper to be multi-threaded and it would probably run at least 20x faster. Writing multi-threaded apps can be somewhat of a challenge, but the very first Python app I wrote was mutlti-threaded page scraper. And you could simply call the Python script from within your PHP page by using one of the shell execution functions.
PHP 可能是也可能不是最好的工具,但您知道如何使用它,并且应用程序的其余部分是使用它编写的。这两个品质,再加上 PHP“足够好”这一事实,为使用它而不是 Perl、Ruby 或 Python 提供了非常有力的理由。
如果您的目标是学习另一种语言,那么选择一种语言并使用它。您提到的任何语言都可以完成这项工作,没问题。我碰巧喜欢Perl,但你喜欢的可能不一样。
Symcbean 在他的链接中提供了一些关于如何管理后台进程的好建议。
简而言之,编写一个 CLI PHP 脚本来处理长位。确保它以某种方式报告状态。使用 AJAX 或传统方法创建一个 php 页面来处理状态更新。您的启动脚本将启动在其自己的会话中运行的进程,并返回该进程正在进行的确认。
祝你好运。
PHP may or may not be the best tool, but you know how to use it, and the rest of your application is written using it. These two qualities, combined with the fact that PHP is "good enough" make a pretty strong case for using it, instead of Perl, Ruby, or Python.
If your goal is to learn another language, then pick one and use it. Any language you mentioned will do the job, no problem. I happen to like Perl, but what you like may be different.
Symcbean has some good advice about how to manage background processes at his link.
In short, write a CLI PHP script to handle the long bits. Make sure that it reports status in some way. Make a php page to handle status updates, either using AJAX or traditional methods. Your kickoff script will the start the process running in its own session, and return confirmation that the process is going.
Good luck.
我同意答案说这应该在后台进程中运行。但报告状态也很重要,以便用户知道工作正在完成。
当收到启动进程的 PHP 请求时,您可以在数据库中存储具有唯一标识符的任务表示。然后,启动屏幕抓取过程,并向其传递唯一标识符。向 iPhone 应用程序报告任务已启动,并且它应该检查包含新任务 ID 的指定 URL,以获取最新状态。 iPhone 应用程序现在可以轮询(甚至“长轮询”)此 URL。与此同时,后台进程将更新任务的数据库表示,因为它使用完成百分比、当前步骤或您想要的任何其他状态指示器。当它完成时,它会设置一个完成标志。
I agree with the answers that say this should be run in a background process. But it's also important that you report on the status so the user knows that the work is being done.
When receiving the PHP request to kick off the process, you could store in a database a representation of the task with a unique identifier. Then, start the screen-scraping process, passing it the unique identifier. Report back to the iPhone app that the task has been started and that it should check a specified URL, containing the new task ID, to get the latest status. The iPhone application can now poll (or even "long poll") this URL. In the meantime, the background process would update the database representation of the task as it worked with a completion percentage, current step, or whatever other status indicators you'd like. And when it has finished, it would set a completed flag.
您可以将其作为 XHR (Ajax) 请求发送。与普通 HTTP 请求不同,客户端通常不会有任何 XHR 超时。
You can send it as an XHR (Ajax) request. Clients don't usually have any timeout for XHRs, unlike normal HTTP requests.
我意识到这是一个很老的问题,但想尝试一下。该脚本尝试解决最初的启动要求以快速完成并将重负载分解为较小的块。我还没有测试过这个解决方案。
I realize this is a quite old question but would like to give it a shot. This script tries to address both the initial kick off call to finish quickly and chop down the heavy load into smaller chunks. I haven't tested this solution.
我想提出一个与 symcbean 略有不同的解决方案,主要是因为我有额外的要求,即长时间运行的进程需要作为另一个用户运行,而不是作为 apache / www-data 用户运行。
使用 cron 轮询后台任务表的第一个解决方案:
使用 Linux inotify 工具的第二种解决方案:
一些附加信息可以在我的帖子中找到:http://inventorsparadox.blogspot.co.id/2016/01/long-running-process-in-linux-using-php.html
I would like to propose a solution that is a little different from symcbean's, mainly because I have additional requirement that the long running process need to be run as another user, and not as apache / www-data user.
First solution using cron to poll a background task table:
Second solution using Linux inotify facility:
Some additional info could be found in my post : http://inventorsparadox.blogspot.co.id/2016/01/long-running-process-in-linux-using-php.html
我已经用 Perl、双 fork() 和与父进程分离做了类似的事情。所有http 抓取工作都应该在forked 进程中完成。
I have done similar things with Perl, double fork() and detaching from parent process. All http fetching work should be done in forked process.
使用代理来委托请求。
Use a proxy to delegate the request.
我总是使用这些变体之一(因为不同版本的 Linux 对于处理输出有不同的规则/某些程序输出不同):
变体 I
@exec('./myscript.php \1>/dev/null \2>/dev/null &');
变体二
@exec('php -f myscript.php \1>/dev/null \2>/dev/null &');
变体 III
@exec('nohup myscript.php \1>/dev/null \2>/dev/null &');
您可能还没有安装“nohup”。但例如,当我自动化 FFMPEG 视频转换时,输出接口在某种程度上并没有通过重定向输出流 1 和 1 来 100% 处理。 2,所以我使用 nohup 并重定向输出。
what I ALWAYS use is one of these variants (because different flavors of Linux have different rules about handling output/some programs output differently):
Variant I
@exec('./myscript.php \1>/dev/null \2>/dev/null &');
Variant II
@exec('php -f myscript.php \1>/dev/null \2>/dev/null &');
Variant III
@exec('nohup myscript.php \1>/dev/null \2>/dev/null &');
You might havet install "nohup". But for example, when I was automating FFMPEG video converstions, the output interface somehow wasn't 100% handled by redirecting output streams 1 & 2, so I used nohup AND redirected the output.
如果您的脚本很长,则借助每个任务的输入参数来划分页面工作。(然后每个页面就像线程一样)
即,如果页面有 1 个 lac Product_keywords 长进程循环,则代替循环为一个关键字创建逻辑,并从 magic 或 cornjobpage.php 传递此关键字(在下面的示例中),
对于后台工作人员,我认为您应该尝试这种技术,它将有助于调用您喜欢的页面数量不限,所有页面都将立即独立运行,而无需异步等待每个页面响应。
cornjobpage.php //mainpage
testpage.php
PS:如果您想以循环方式发送网址参数,请按照以下答案操作:https://stackoverflow.com/a/41225209/6295712
if you have long script then divide page work with the help of input parameter for each task.(then each page act like thread)
i.e if page has 1 lac product_keywords long process loop then instead of loop make logic for one keyword and pass this keyword from magic or cornjobpage.php(in following example)
and for background worker i think you should try this technique it will help to call as many as pages you like all pages will run at once independently without waiting for each page response as asynchronous.
cornjobpage.php //mainpage
testpage.php
PS:if you want to send url parameters as loop then follow this answer :https://stackoverflow.com/a/41225209/6295712
正如许多人所说,这不是最好的方法,但这可能会有所帮助:
Not the best approach, as many stated here, but this might help:
如果您的脚本所需的输出是某种处理,而不是网页,那么我相信所需的解决方案是从 shell 运行您的脚本,就像
php my_script.php
If the desired output of your script is some processing, not a webpage, then I believe the desired solution is to run your script from shell, simply as
php my_script.php