Why does infinity while loop stop page from loading

I have a code that runs in an infinity loops checking for events for me. and i control its stop using values from a database, but the problem is once i start the loop no other page will load or can be viewed until the loop is stopped.

set_time_limit(0)
$control = '';

while($control != 'stop'){
// keep checking for events functions goes here

$control = $search->query("SELECT status FROM  mytable WHERE id = 1");
usleep(2000000);
}

Everything works perfectly but the problem is that other pages of my site does not load they will just be showing a loading spinning but no content is outputted, until i stop the while loop.

Is there a solution to this please?
secondly if i have an ajax call that posts to a file or url located in this web while the loop is still spinning can i get response or everything have to wait till i stop the infinity loop?

You can insert a

session_write_close();

At the top of this script if you are sure, that there is no need to write to any Session variable after.

1 Like

there will be need for some session variables.

secondly if i choose to close or end the session will the loop still be running at the background while users go on accessing the page or pages without being affected by the loop?

Lets say my website directory are as follows
index.php
aboutus.php
contact.php
task.php —which is the php file that has the while loop on it.

and i choose to add the session_write_close() at the top of task.php

  1. will it end other session from the other pages or just only affects task.php?

It will only affect the task.php and yes when you close the session for this file the other files will be executed, because PHP is no longer waiting for the completition of task.php before it executes the next called file.

1 Like

Noone gonna mention the missing quotation to close the query string?

thanks, i have closed the query string

I have tried it out and found that it was actually a session issue, so closing the session helped as you directed.

Alternatively, closing the browsers and opening a opening it again works. Or using a different browser.

Thanks alot.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.