如果超过给定时间则停止运行 php 函数

发布于 2024-12-06 11:30:48 字数 336 浏览 1 评论 0原文

我需要使用 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 技术交流群。

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

发布评论

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

评论(2

各自安好 2024-12-13 11:30:48

这并不像您想象的那么容易完成。由于您仅在一个线程上运行,因此您无法进行任何检查。如果该线程正在阻塞,则它正在阻塞。

您需要创建某种多线程环境,在其中运行一个工作线程来执行 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.

甲如呢乙后呢 2024-12-13 11:30:48

@klaus 所说的纳入其中帐户,如果您可以编辑 parse_html() 函数,您能够执行此检查。在函数内,可能存在对各种子函数的大量调用或大量 for 重复循环。您想要在函数中的某处或 for 循环的头部添加检查,以查看函数的执行时间是否过长。

简单的伪代码示例:

function parse_html()
    start_time = 0;

    read file

    foreach element_to_be_parsed
        runtime = current_time - start_time
        if runtime > (whatever)
            break
        end

        ...do parsing stuff
    end
end

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 of for repeat loops. You want to add a check somewhere in the functions, or at the head of the for loops, to see whether the function is taking too long to execute.

Simple pseudocode example:

function parse_html()
    start_time = 0;

    read file

    foreach element_to_be_parsed
        runtime = current_time - start_time
        if runtime > (whatever)
            break
        end

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