Timeout a function in PHP

后端 未结 7 2018
时光说笑
时光说笑 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 07:50

    Before starting into the loop, set a variable that remembers the time(). At the end of each iteration, check if the current time() is more than 60 seconds greater. If so, break. For instance:

     $value){
        some_function($key, $value); //This function does SSH and SFTP stuff
        if (time()-$startTime>60) break; //change 60 to the max time, in seconds.
    }
    ?>
    

提交回复
热议问题