by konrness.com
Many people are aware that modern browsers limit the number of concurrent connections to a specific domain to between 4 or 6. This means that if your web page loads dozens of asset files (js, images, css) from the same domain they will be queued up to not exceed this limit. This same problem can happen, but even worse, when your page needs to make several requests to PHP scripts that use sessions.
Problem
PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()), this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts, for instance, for loading content via Ajax, each request could be locking the session and preventing the other requests from completing.
The other requests will hang on session_start() until the session file is unlocked. This is especially bad if one of your Ajax requests is relatively long-running.
Solution
The session file remains locked until the script completes or the session is manually closed. To prevent multiple PHP requests (that need $_SESSION data) from blocking, you can start the session and then close the session. This will unlock the session file and allow the remaining requests to continue running, even before the initial request has completed.
To close the session, call:
session_write_close();
This technique works great if you do not need to write to the session after your long-running process is complete. Fortunately, the $_SESSION data is still available to be read, but since the session is closed you may not write to it.
Example
<?php
// start the session
session_start();
// I can read/write to session
$_SESSION['latestRequestTime'] = time();
// close the session
session_write_close();
// now do my long-running code.
// still able to read from session, but not write
$twitterId = $_SESSION['twitterId'];
// dang Twitter can be slow, good thing my other Ajax calls
// aren't waiting for this to complete
$twitterFeed = fetchTwitterFeed($twitterId);
echo json_encode($twitterFeed); ?>
Hey there, You have done a fantastic job. I’ll certainly digg it and in my opinion suggest to my friends. I’m sure they’ll be benefited from this site.
Thanks a lot! 🙂
Awesome issues here. I am very happy to see your article.
Thanks so much and I am having a look forward to touch you.
Will you please drop me a mail?
If you need you can contact me with twitter.
I think the admin of this web site is really working hard
in favor of his site, since here every stuff is quality based material.
Hey I am so glad I found your blog, I really found
you by mistake, while I was researching on Google for something else, Anyways I
am here now and would just like to say kudos for a remarkable post and a all round
exciting blog (I also love the theme/design),
I don’t have time to look over it all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the superb job.
Thank you!!
Did you copied the post from http://konrness.com/php5/how-to-prevent-blocking-php-requests/ .
If so good to link to the original.
Thanks
If you see, at begin of article there is already a link to the original post.