Set timeout for specific function

Asked

Viewed 188 times

4

I have a script with several functions.

I would like to set one timeout (guy set_time_limit) specific to a function, and if time runs out, it "disregards" and continues running the loop/script.

Example:

function teste(){
    sleep(10);
}

foreach($array as $v) {
    echo $v;
    teste();
}

Taking the example, the function teste() will run for 10 seconds at each loop.

So want to set a timeout 3 seconds to function, and in the event of "bursting" time, he dismisses, and continues the loop.

  • Is there such a possibility?
  • The Reactphp library provides ways to implement this functionality. With specific PHP functions, I believe you will not get this behavior. Reactphp

1 answer

0


My curiosity led me to this answer. I don’t know if it’s the best solution, but it works. To do what you want I got using execphp to run the file containing the function teste() in the background. Thus:

$array = [1,2,3,4,5,2,1];

foreach($array as $v) {
    $var = exec('C:\xampp\php\php.exe  '. __DIR__ .'\teste.php '.$v, $saida);
    echo $var;
}

Filing cabinet php test.

error_reporting (0);
set_time_limit(3); 
function teste($sec){
    sleep($sec);
    echo "executou > $sec - ";
}
$sec = $argv[1];
teste($sec);

The variable $v is released as an argument for the script that is retrieved in $sec.

Upshot

executou > 1 -executou > 2 -executou > 3 -executou > 2 -executou > 1 -
  • 1

    Although I find valid idea (although I do not answer directly to the question), what you did there is to change the crontab or a task scheduler for a PHP (I am not saying that this is bad, I just find it interesting to observe. ). If your idea is well implemented, you can make a "crontab configured by DB", for example (I even suggested this in an old answer of mine).

  • @Bacco true! A crontab would be an interesting option.

  • I meant the opposite, you take advantage of your idea and make a "crontab" to use with PHP (but not for the question case). Because the original crontab you have to work with the shell, a PHP can serve as a scheduler for a system in PHP configurable per panel, pq being run by the shell it can have infinite loop (as long as it has a Sleep to release cpu for other things, of course).

  • @Bacco understood. I never used cron, I just heard about it. It seems to me that it is not dynamic right? You set a certain date and time it executes a certain script?

  • 1

    It is a service that every minute can perform a task, and you configure it like this: https://answall.com/questions/124518/70

  • 1

    On each line you put a time setting, an executable and its arguments. Every minute the crontab will check the schedule list and execute whatever is appropriate for that moment.

  • For example, the line task one every Saturday, every half hour. the line two every Monday, Wednesday and Friday at 12h and 19h30... The one on line 3, runs every day every 7 minutes, and so on.

  • @Legal Bacco. It is a good solution to run scripts for backup database update...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.