Timeout a function in PHP

后端 未结 7 2020
时光说笑
时光说笑 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:00

    PHP is single threaded... you have to use your OS's ability to fork another process.

    The only way I know how to do this is with the exec command to fork off another full process. Put the stuff you want timed in a separate script, call exec on the script, then immediately sleep for 10 min. If at 10 min the process is still running kill it. (you should have it's PID by making the command run the new php code in the background, getting it via command line and returning it from the exec command).

提交回复
热议问题