Timeout a function in PHP

后端 未结 7 1973
时光说笑
时光说笑 2020-11-29 07:43

Is there a way to timeout a function? I have 10 minutes to perform a job. The job includes a for loop, here is an example:



        
相关标签:
7条回答
  • 2020-11-29 08:07

    What you want is to use the pcntl_alarm function, which will trigger a SIGALRM signal on timeout.

    For example:

    // 10 minutes
    pcntl_alarm( 600 );
    pcntl_signal(SIGALRM, function() { print( "Timed-out!\n" ); exit( 0 ); });
    

    Please see here the PHP manual for more on this: http://php.net/manual/en/function.pcntl-alarm.php

    0 讨论(0)
提交回复
热议问题