Long Polling. Looong delays


We implement an API based on YII and using long polling scheme. Browser creates several ajax-requests. Server replies in JSON. Some actions reply immidiately, some are holding the connection using usleep while they have nothing to say.

Such a problem occured:

When creating more than one parrallel ajax request, time of execution increases tooo much. Actions that should answer immidiately sometimes take 20-30 sec to run. I actually added code that exchoes to console current unix time in index.php and in my action (in the controller) and got interesting results:

time passed between starting of application and actual running of the action varies between 20-30 sec.

In fact the is no highload to DB and DB located on localhost. We’re using PostGre, but MySql give the same.

Also, as I said when passing ajax-requests one-by-one they work as expected.

Please help, what can be the problem…

Hmm, I doubt it’s Yii as the requests won’t be interacting with each other. Try isolating the queries that are being run and putting them in a sandbox to remove Yii from the equation. Could it possibly be that some of the sleeping queries are locking tables? Then the ones that should execute have to wait for the sleeping queries to finish before they are allowed to read? Sounds like you have plenty of time to get status for queries while you perform the calls.

In fact I have isolated the problem:


mysql_connect("localhost", "root", "") or

   die ("Не могу подключиться к серверу MySQL!");

mysql_select_db("speakto") or

     die ("Не могу подключиться к базе данных");

$sql = "SELECT * FROM users";


	$result=mysql_query("SELECT * FROM users")

		or die("Не могу выполнить запрос!");



		print $row["email"].",".$row["email"]."<br>";





echo 'execution time: '.(time()-$startTime);

as you see - only select queries. But two instances of that script can not run together… the second instance has to wait first one to finish. Max connection in phpMyadmin equals to 151