如果超过给定时间则停止运行 php 函数
我需要使用 php 解析许多 html 文件。
foreach($url_array as $url){
$file = file_get_contents($url);
parse_html($file);
}
由于某些原因(文件太大),函数 parse_html() 需要很长时间才能运行或存在内存泄漏。
我想监视函数parse_html()。如果运行时间超过给定时间,则应继续解析下一个网址并忽略当前网址。
大多数时候,我的代码运行良好,但有一些网址无法解析。没有错误输出,我猜是内存泄漏。
I need to parse many html files using php.
foreach($url_array as $url){
$file = file_get_contents($url);
parse_html($file);
}
For some reasons (file is too big), function parse_html() take very long time to run or has memory leak in it.
I want to monitor function parse_html(). If the running time exceed a given time, should continue to parse the next url and disregard the current one.
For most of the time, my codes runs great but there are some urls can not be parsed. There is no error output and I guess it is memory leak.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不像您想象的那么容易完成。由于您仅在一个线程上运行,因此您无法进行任何检查。如果该线程正在阻塞,则它正在阻塞。
您需要创建某种多线程环境,在其中运行一个工作线程来执行
parse_html()
(为了提高速度并利用多核处理器,您甚至可以生成更多工作线程)和另一个线程,用于检查并杀死工作人员(如果他们花费了太多时间)。This can not be done as easily as you think. Since you are running on one thread only, you cannot have any checks. If this thread is blocking, it is blocking.
You need to create some sort of multi-threaded environment where you run one worker thread for the execution of
parse_html()
(to increase speed and take advantage of multi-core processors you could even spawn more worker threads) and another thread that checks and kills the workers if they are taking too much time.将 @klaus 所说的纳入其中帐户,如果您可以编辑
parse_html()
函数,您将能够执行此检查。在函数内,可能存在对各种子函数的大量调用或大量for
重复循环。您想要在函数中的某处或 for 循环的头部添加检查,以查看函数的执行时间是否过长。简单的伪代码示例:
Taking what @klaus said into account, you would be able to perform this check if you can edit the
parse_html()
function. Within the function, there are likely either a number of calls to various subfunctions or a large number offor
repeat loops. You want to add a check somewhere in the functions, or at the head of thefor
loops, to see whether the function is taking too long to execute.Simple pseudocode example: