Can I set a time limit for a code block?

Is it possible to run a block of code (maybe just call a function), and if it does not execute for a certain time, skip it.

//give this function 10 seconds to execute
$value = mega_function();// could take anything from 1-1000 seconds
//if 10 seconds have passed and the value is still not set, abort it and set $value = false;
+5
source share
2 answers

No. You have to either

  • Calling a function inside an external file using curl or file_get_contents()- you can set the timeout there

  • If necessary, track the time inside mega_function()and return().

What does it do mega_function()?

+7
source

Try to study streams, but it can be inconvenient to do something similar in PHP :)

http://php.net/manual/en/function.pcntl-fork.php pnctl , , .

0

All Articles