I create function for if no get one value - in this case ok - repeat and recheck other time and other time , but the problem it´s all time when check 2 or 3 times finally put me connection down
My function :
<?php
function recursive() {
if (file_exists("pol.txt")) {
print "ok";
} else {
print "bad";
usleep(1);
recursive();
}
}
recursive();
?>
The idea it´s the function test this all time and stop when other function create the file called pol.txt , but the problem it´s the function , because all time in 2 seconds put me coonection down in all brownsers
I test actually in localhost and in server with the same results
The question it´s if no possible run this kind of function or exists other way for do this without refresh page each time , because i can´t use refresh in this case
Thank´s , regards
The problem is that you are using usleep(). That delays the execution in micro-seconds so in two seconds you will have a very deep level of recursion, causing your script to error out.
For this you should probably just use a loop and make sure it ends after an x-number of seconds or iterations.
Related
I'm writing a function that supposed to update the price for all woocommerce products.. the price data i will get from amazon using amazon api which has a certain x query per second limit thats why i will have to sleep on each loop. Im planing to run that function as a cron job. the below function is just an example for what im planing to do so please ignore he missing variable declaration etc..
I understand that i can increase the php timeout limit but im imagining what if i have hundreads or thousand of products and on each query i will have to to sleep for a while to avoid query throtling so to update all products it can take hours in this case so im wondering what is the best and easiest solution to keep that function looping for hours and stop after reaching the last id on $products_ids array?
function text(){
foreach ($products_ids as $products_id) {
//apm_single_lookup func do an API call which has max query/sec limit thats why i added the next sleep
$lookup_data = apm_single_lookup($eu_asin, $amazon_domain);
update_post_meta($products_id, $field_name, esc_attr($lookup_data['price']));
sleep(1);
}
}
You can change max_execution_time of the server.
Or use :
http://php.net/manual/fr/function.set-time-limit.php
like this :
set_time_limit(3600);
function text(){
...
}
Or another solution :
Split your loop in multiple Ajax call (or cronjobs), so you can stop and go and do what you want.
Like this:
function text(){
foreach ($products_ids as $products_id) {
set_time_limit(60); //timelimit per loop iteration
//apm_single_lookup func do an API call which has max query/sec limit thats why i added the next sleep
$lookup_data = apm_single_lookup($eu_asin, $amazon_domain);
update_post_meta($products_id, $field_name, esc_attr($lookup_data['price']));
sleep(1);
}
//set_time_limit(5*60); //set back to longer after the loop if you want etc.
}
I think it's better in the case of loop to reset the timer on each iteration.
When called, set_time_limit() restarts the timeout counter from zero.
This way you can keep the timout small per iteration and not worry if it you have a lot of iterations. That made more sense in my head ....
It's even better to do it in the CLI (Command Line). Even when you set PHP's max execution time, Apache has it's own time limits too (likely in mod_fcgi etc).
put this code before loop:-
ini_set('max_execution_time', 0);
or
set_time_limit(0);
Is it possible to set a delay between 2 portion of code in PHP? I want something like this-
<?php
function firstFunc(){
echo "anything";
}
function secondFunc(){
echo "something";
}
// call first fumction
firstFunc();
// I want the script to wait for 2 seconds here, then call second function
secondFunc();
Is it possible?
If you want to delay the execution of something, you can use the sleep($seconds) function. Here's what the documentation says
Delays the program execution for the given number of seconds.
It returns zero on success and false on error.
So you can do something like:
<?php
function firstFunc(){
echo "anything";
}
function secondFunc(){
echo "something";
}
// call first function
firstFunc();
sleep(2); //Delays execution for 2 seconds.
secondFunc();
?>
Keep in mind though, PHP is a server side language. The only thing the client will receive from a PHP script(if the client has to receive anything at all) is HTML(or CSS, JS and so on). So when you run this code, the page will show nothing for 2 seconds and then output anything and something together because the delay is happening at a server level, and not on client machine. If you want a kind a delay where something is visible on the screen and then after 2 seconds anything appears, you want to use JavaScript.
Hope this helps :)
You should use function sleep(int seconds)
I have this little script and function:
function control($value,$position)
{
if ($_SERVER['REMOTE_ADDR']=="".$value."" && $position==0)
{
print "".$value." $x<br>";
return;
}
else
{
control($value,$position);
usleep(20);
}
}
$x=0;
foreach($aa as $aaa)
{
$exp=explode("-",$aaa);
$exp2=explode("_",$exp[1]);
control($exp2[0],$x);
$x++;
}
The loop takes values from a file and sends them to a function to verify, in the function for example if value is the same as the IP of a user it works and if not it continues to execute the function. If not, the idea it repeats to continue verification; the function stops when finally the function verifies this value and it is ok.
I need this to work in one only load, for this I am thinking to use the sleep and repeat functions because the time difference for verification is very low, but in some moments more than 3 users can need to verify this.
The most important for me is to know I can do this in the function because if I use it in this way all the time the page tells me the connection is down, and I only need to repeat the function when the verification result is not the same, and if it is not the same repeat the function another time inside the loop and sleep and repeat in the wait mode.
Sorry but I try to tell all this the best I can.
Regards and thanks.
Im facing some issues with a longrunning loop.
It goes through around 40.000 urls that it scrapes. however, some external pages can be unreachable at that exact time etc.
If it throws maximum execution time, the whole function stops. so im at a loss of how to go around that.
been scourging the net for answers and found how to catch the timeout exception. so did a function like this :
ini_set('max_execution_time',15 );
function shutdown()
{
$a=error_get_last();
if($a==null)
DoMyStuff();
else
DoMyStuff();
}
register_shutdown_function('shutdown');
foreach blabla {
shutdown();
}
My thoughts is that it will catch a timeout, and give it another attempt if it does.
But that's not really good enough.
Optimally i would want a script that tries to do my function 3 times and if it times out 3 times- then go on to next in loop.
but i cannot for the life of me figure out how to do that.
Any help is greatly appreciated SO! =)
I have a block of code and want to run it after certain time intervals, say after every 3 seconds. If I get required result it should die otherwise another request will be sent after 3 seconds. Any idea to do it with php.
Thanks in advance for any help
You can sleep in a loop:
while (true) {
$result = doSomething;
if (resultIsGood) {
break;
}
sleep(3);
}
If you don't want to keep the browser waiting, you can look up ignore_user_abort() solutions on Google.
If what you want to execute MySQL queries (and nothing else), maybe using the MySQL event scheduler could fit your need:
http://dev.mysql.md/doc/refman/5.5/en/events-overview.html