1

I'm trying to get two instances of a php script to run concurrently. I have a script, 'test.php':

<p><?php echo time(); ?> Sleeping...</p>
<?php sleep(5); ?>
<p><?php echo time(); ?> done</p>

If I load the page in two browser tabs at the same time, I get this:

1446855680 Sleeping...

1446855685 done

and this:

1446855686 Sleeping...

1446855691 done

One instance blocks until the other loads. This happens on both Firefox and Chromium.

If I make a second identical script, 'test2.php', or rewrite two urls to the same script, and load the two pages in different tabs, I get this:

1446855862 Sleeping...

1446855867 done

and this:

1446855863 Sleeping...

1446855868 done

Both instances are loading at the same time. So it is identical URLs that are being blocked.

How can I get two instances of a script with the same URL to load/run at the same time?

Jon Hulka
  • 1,259
  • 10
  • 15
  • Since I do not see any actual synchronisation in your skript, have you checked it is not a browser optimisation (have you tried loading one instance in firefox and the other instance in chrome?). If it is a browser optimization, try adding a random parameter (`?random=123`) (with a *different* value) to the page loads and see what happens. – ted Nov 07 '15 at 01:08
  • 2
    This is not about php's behaviour but the browser's. Both firefox and Chrome/-ium perform (cachable) requests for the exact same url in sequence. – VolkerK Nov 07 '15 at 01:15
  • @ted the page will load concurrently in Firefox and Chromium. The random parameter also allows it to load concurrently. – Jon Hulka Nov 07 '15 at 01:27
  • @VolkerK is there any way to tell the browser the page is not cache-able before it is loaded? I tried `header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); flush();` but that didn't work – Jon Hulka Nov 07 '15 at 01:35
  • @Fred-ii- It is on a remote server. On both browsers (Chromium and Firefox) when loading the same url in separate tabs/windows (same URL, same browser, different window), one instance always finishes before the next one starts. – Jon Hulka Nov 07 '15 at 01:42
  • Appending a random value to the url query string might help, i.e. instead of querying http://localhost/test.php twice call http://localhost/test.php?9723592364923864392746 and http://localhost/test.php?5645400320422234392914 – VolkerK Nov 07 '15 at 09:26
  • Given that you confirmed my suspicion,/Volkers hint with cacheable requests, the question is why you would need to bypass this. Is there any case in which your site generates different data or where it is desireable that you load the same page twice? If so follow the random parameter suggestion – ted Nov 07 '15 at 22:25
  • @ted I'm implementing a window-specific session data handler. I just need ajax calls from different windows to run concurrently. Your parameter suggestion works for my purposes. If you write it as an answer I will accept it. – Jon Hulka Nov 08 '15 at 01:43

1 Answers1

0

As VolkerK pointed out and your testing confirmed Firefox/Chrome perform requests for the exact same URL in sequence. However in practice this is not a problem since Users usually do not load the same page twice, and for a script that serves data to Ajax requests the URLs will differ since different parameters are appended.

Should you want to bypass this mechanism in general you can append a random parameter (has to be different in the different browser tabs) to the url, i.e. url.to/your/script?urlID=4821de7e524cf762deab6ed731343466. The random parameter causes the browser to see two different URLs and thus to perform the load in parallel.

ted
  • 4,791
  • 5
  • 38
  • 84