Jump to content

PHP Script can't stop execution


php_nub_qq

Recommended Posts

Hello freaks. I'm in need of assistance because I just figured out my whole system might turn out to be a big load of yeah..

 

I have created a chat system which uses polling to update stuff, except that instead of returning a "no new messages" response it loops through the script again. It looks something like this

 

while(true){

    Check for messages;

 

    if(messages) break;

}

 

What the problem is ( that I'm aware of ) is that I can't stop the script unless it returns new messages ( executes on it's own until the end ). Even if I remotely call the xHttp request's abort() method it only gets aborted on the client side, but the script keeps running. After looking for solutions I came up with this

 

 

ignore_user_abort(false);

while(!connection_aborted(){

 

 

}

 

which was supposed to exit the loop when the client closes their browser, but it doesn't.

 

The only thing I can do is have max execution time stop the script, but this script is supposed to run for like at least 15 minutes before timing out, and if the client closes their browser on the 5th second there goes 14 minutes of waste

 

I'm in a desperate need of help, please!

 

Link to comment
Share on other sites

That is what I am trying to avoid, sending empty requests. I think I'm on the path to solve my issue. Apparently php's connection handling gets updated only when data is outputted from the server so now I'm flushing data out but it is still not working. I'm now working on a custom xhr onreadystatechange function to receive data in pieces, to see if data is actually sent, since I'm using jquery and it's readystate handler cannot be modified from what I've read. Going to keep you updated on my progress

Edited by php_nub_qq
Link to comment
Share on other sites

I just found out what the problem is: if there is any requests to a database in the loop PHP doesnt push any data with flush() and ob_flush() even if it is a simple echo, does anyone have a solution to that?

Edited by php_nub_qq
Link to comment
Share on other sites

A database request shouldn't affect how flush/ob_flush work. If they are not working properly then most likely there is something else down the line buffering your data which you have no control over (ie, the webserver, proxy, cache server, etc).

 

Also I don't know what your full code is, but if you are going to do a while(true) loop like that then you should include a delay in each iteration. That way even if your script does end up running for a long time it is not chewing up all the CPU cycles and DB processing checking for nothing. Something like:

while (!$messages = GetNewMessages()){ 
    sleep(1); //Pause for a second
}
If you really want to do long-polling then a more ideal solution would be to have a separate server which the clients connect to for message checking which handles the situation better. The main problem with doing this with something like apache is that while your script is sitting there waiting for new messages, it's tying up processing threads which apache uses to handle requests. If you end up tying up all the processing threads with your polling script nobody would be able to browse your site at all. That is one of the reasons that short-polling (ie, returning a 'nothing new' response, despite being a bit of a bandwidth waste, is preferred.
Link to comment
Share on other sites

Of course I have a delay. I can confirm that the problem is with the database requests because if I remove them it works, see:

 

 

doesn't work

	while(true){
		$messages = $msg->getMessages($ajaxData);

		if(!empty($messages)){
			$break = true;
		}
		
		$conversation = $msg->getConversations($ajaxData[2], $ajaxData[4]);
		
		if(!empty($conversation)){
			$break = true;
		}

		$loop++;
		if($loop > (10/$interval) || $break){ break; }
		
		usleep($interval*1000000);

                echo json_encode(array('response' => 'test'));
	}

works

	while(true){
		//$messages = $msg->getMessages($ajaxData);

		if(!empty($messages)){
			$break = true;
		}
		
		//$conversation = $msg->getConversations($ajaxData[2], $ajaxData[4]);
		
		if(!empty($conversation)){
			$break = true;
		}

		$loop++;
		if($loop > (10/$interval) || $break){ break; }
		
		usleep($interval*1000000);
		
		echo json_encode(array('response' => 'test'));
	}

"works" meaning that it pushes data partially.

 

EDIT: all those methods do is verify the data given then connect to the DB, retrieve and return the data

Edited by php_nub_qq
Link to comment
Share on other sites

While what is true include a loop counter beyond the preset limit?  You should really do a limit per page on the site with mysql.

 

 

	while(true){
		$messages = $msg->getMessages($ajaxData);

		if(!empty($messages)){
			$break = true;
		}
		
		$conversation = $msg->getConversations($ajaxData[2], $ajaxData[4]);
		
		if(!empty($conversation)){
			$break = true;
		}

		$loop++;
		if($loop > (10/$interval) || $break){ break; }
		
		usleep($interval*1000000);

                echo json_encode(array('response' => 'test'));
	}

 

        $loop++;

        if($loop > (10/$interval) || $break){ break; }

Edited by php_nub_qq
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.