5

Is it possible to start a block of code (maybe just call a function) and if it doesn't execute within 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;
j0k
  • 22,600
  • 28
  • 79
  • 90
Moak
  • 12,596
  • 27
  • 111
  • 166

2 Answers2

7

No. You would have to either

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

  • Keep track of the time inside mega_function() and return() if necessary.

What does mega_function() do?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • mega function is a program trying to interpret various blocks of text, so in order to get quicker results we just want to skip the ones that need more time for now, i guess I need to optimize code and probably use another language than php, but yea, got my tools – Moak May 11 '11 at 07:15
  • 1
    If it actually calls an external program, you can keep track of the time and kill the external process inside `mega_function()` – tobyodavies May 11 '11 at 07:17
  • You may also launch a process from PHP, retrive the PID and relase the control. Then check until some seconds the PID status from an external script. – Fabio Mora May 11 '11 at 07:33
0

Try looking into threads, but it might be awkward to do something like this in PHP:)

Look at http://php.net/manual/en/function.pcntl-fork.php and all pnctl related functions for creating childs, sending signals between them, waiting for child to finish or killing threads.

Quamis
  • 10,924
  • 12
  • 50
  • 66