Cupidvogel Posted May 25, 2012 Share Posted May 25, 2012 Hi, I was reading an article on long-polling at http://www.nolithius.com/game-development/comet-long-polling-with-php-and-jquery where I found the following lines (scroll down a little in that page to view this, under the PHP sleeps across the entire session heading): When you make a call to sleep() or usleep(), PHP will pause execution across the entire session, which means that any other AJAX requests or even pageloads will have to wait until the request returns or times out. To address this, ensure the session is closed before entering the serverside sleep loop. I didn't quite understand what it means. Suppose I have a webpage foo.php, and it contains two updateable parts, one calls coo.php to update asynchronously, while the other calls goo.php. Now both coo.php and goo.php themselves poll the database, returns the result set if they get any, or wait for, say, 15 seconds before polling the database again, and this continues, until the total execution time exceeds 100 seconds, upon which PHP results the empty (or populated) result-set. The Javascript on the client side receives that empty or populated data and immediately sends another request. Now aren't these two requests to 2 different scripts (albeit from the same script) independent of each other, or is it that when coo.php sleeps, foo.php and goo.php is forced to sleep as well? Quote Link to comment https://forums.phpfreaks.com/topic/263122-long-polling-with-php/ Share on other sites More sharing options...
kicken Posted May 25, 2012 Share Posted May 25, 2012 The issue is if you are using sessions (ie, call session_start()). PHP Locks the file that stores the session data so only one script/process can access it at a time. So if you make a request to coo.php and it starts the session then PHP locks the file. While it is sitting there doing it's polling your request to goo.php will be blocked at the session_start() call waiting for coo.php to unlock the session file so it can use it. There are three ways to avoid this problem. 1) Don't use session in your long poll scripts, not very useful as you often need the session data. 2) Call session_write_close() to end the session and free the file. Your $_SESSION variable will still exist but you won't be able to write to the session. 3) Implement your own session handler using something like Mysql or Memcached so that there is no longer an issue with locks. #3 would be the ideal solution. #2 will work in most cases since generally you only need to read the session data for login/user id details and not write anything. Quote Link to comment https://forums.phpfreaks.com/topic/263122-long-polling-with-php/#findComment-1348680 Share on other sites More sharing options...
Cupidvogel Posted May 26, 2012 Author Share Posted May 26, 2012 Please explain solutions 2 and 3 in detail, I am kind of new to PHP! When should I call session_write_close? What is meant by session data? I mean, once the user is logged in, how can a script log out of it during a polling request while the user still continues to browse other pages and expect the pages to be personalized? How come MySQL and memcache help here? Quote Link to comment https://forums.phpfreaks.com/topic/263122-long-polling-with-php/#findComment-1348721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.