2

Good day.

For first, sorry for my bad English =)

So. I created script:

<?
sleep(10);
?>

My Apache has MPM module, I obviously didn't use sessions in this script, just.. just sleep(10). When I open 2 tabs in my browser simultaneously, first tab loads in 10 seconds, second tab - 20 seconds.

But. When I open this script in 2 different browsers at the same time, it loads in each one after 10 seconds.

So, I started thinking, that my problem is "Connection: Keep-Alive". I changed my script:

<?
  header('Connection: close');
  phpinfo();
  sleep(10);
?>

phpinfo() - to be sure, that headers were sent before sleep(). Buuuut... I meet the same problem. In first tab of Chrome I get headers with "Connection: close", in second tab I can't get response headers while first script is not ended. In two different browsers - everything is normal.

And now I have absolutely no ideas what I'm doing wrong. Why Chrome can't make 2 parallel queries to my site? What I should do to solve this problem?

P.S. I don't want to disable keep-alive for all my site. I don't mind, if it will speed up loading of css, images and other stuff. Even other scripts. But I want to have ability to run some scripts parallel in one browser. P.P.S. For example: at the one page will be very long ajax query, for example - processing some big data at server-side and ajax queries with some little interval - to get status of executing first query. Obviously, that they must be parallel.

  • http://stackoverflow.com/questions/12101082/php-sleep-function-email-throttling-freezes-entire-server – Hanky Panky Mar 08 '13 at 07:21
  • Id guess it was your apache settings take a look at the settings for [KeepAliveTimeout](http://httpd.apache.org/docs/2.2/mod/core.html#keepalivetimeout) and [MaxKeepAliveRequests](http://httpd.apache.org/docs/2.2/mod/core.html#maxkeepaliverequests) – prodigitalson Mar 08 '13 at 07:30
  • @HankyPankyㇱ thank you, but it's not really what I want. I know about cron, but I don't want to use it right here =) If I will not find any other solution - I'll have to use cron =( – Danila Simonov Mar 08 '13 at 08:12
  • @prodigitalson thank you. I tried to set KeepAlive Off for all my site. And with WebKit Inspector I see, that Chrome really get "Connection: close" header, but the problem is not solved - Chrome didn't make NEW connection to site, while he didn't loaded page with "Connection: close" header. May be, there is way in ajax mechanism, to say that this ajax query should be in a new connection? – Danila Simonov Mar 08 '13 at 08:19
  • I think it would be interesting if you added some logging instructions in there. – didierc Mar 08 '13 at 22:03
  • Old question but I also replicate this in Chrome and Apache 2.2.22. Even with KeepAlive Off the second tab on chrome will not complete until the first tab is done. I notice this when I do a somewhat long process then try to open page on the same site. Similar issue but doesn't work for me even with unique requests. http://stackoverflow.com/questions/27513994/chrome-stalls-when-making-multiple-requests-to-same-resource – Dustin Butler Sep 23 '15 at 21:05

1 Answers1

8

I know it's an old question but I just had the same problem and solved it with session_write_close()! Without it PHP purposely queues scripts for same session.

Simplest possible example:

Looong Script #1:

<?php

$_SESSION['progress'] = 0;

for ($i=0; $i < 100; $i++)
{
    session_start();
    $_SESSION['progress']++;
    session_write_close();
    sleep(1);// This is slowing script purposely!
}

?>

Short script #2:

<?php
session_start();
print_r($_SESSION['progress']);
?>

Now try it, open first script that takes ages open second script in new tab and get the progress updated in a blink while first still running!! So easy right?! ;)

Same principle for ajax polling long script and second ajax call to get the progress!

antoni
  • 5,001
  • 1
  • 35
  • 44