Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

While testing for a long execution script I came across a strange problem so I tried to test it using a simple script. For sake of testing I made a simple PHP page with below codes.

<?php
sleep(60);
echo 1; 
?>

I ran this page in the browser and while it was executing in the mean time made changes to the code by just simply commenting the sleep function, saved it and then ran the same page in another tab of the same browser. I was expecting the output should come instantly in the second tab but it came only after execution of the code in the first tab.

I repeated the above steps but this time appending random numbers in query string. It worked as expected i.e. though first tab was executing the page second tab echoed the result.

Now I am interested to know why browsers are not executing the same page in two different tabs though the page had changed in the mean time. I tested it in Firefox and chrome and my server is Apache.

share|improve this question
    
the browser is waiting for a response from the first request before it sends the second one, output is not being inserted into the wrong tab. make two scripts have one echo 1 and one echo 2 and run them at the same time. –  cmorrissey Aug 9 '13 at 14:52
    
I have tested with 2 also. But it was still waiting for the first tab response. I want to know why it is waiting because the same is working when i use query string with random numbers for both the tabs. –  Suraj Aug 9 '13 at 14:55
1  
Could it be that the PHP server has the first page cached during the first call's runtime? considering that possibility, the PHP server wouldn't recognize changes to the script until (A) the first call terminates and another initializes or (B) the second one has different request parameters (different query string, for example) so PHP loads a second instance in memory for the "different" page? I'm not sure if I'm on to something, or if I'm way out in left field but maybe someone with a little more expertise could verify the validity (or lack thereof) of my speculation? –  chris.nesbit1 Aug 9 '13 at 14:56

1 Answer 1

I bet you use session. session_start() locks the session file, or waits until it's unlocked by other process. Use session_write_close() as soon as possible.

share|improve this answer
    
Sorry @Marek but I am not using session at all and I am well aware of session_write_close functionality. My code in the page is exactly what I have posted. –  Suraj Aug 9 '13 at 15:01
    
it's a plausible answer, and it's caught out a lot of people so it's worth mentioning. However, given that he's testing across two different browsers, they won't be using the same session, so it is unlikely to be the actual problem here. –  Spudley Aug 9 '13 at 15:02
1  
You are right. Now I'm thinking that browsers have some pool of network connections, and if any of them is matching the requested resourse, it uses (and waits with) the connection. This was image wouldn't be requested twice just because it's referenced 2 times on webpage. –  Marek Aug 9 '13 at 15:14
    
I think what you are saying is correct @Marek. –  Suraj Aug 9 '13 at 15:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.