{"id":50,"date":"2008-02-18T23:08:02","date_gmt":"2008-02-18T20:08:02","guid":{"rendered":"http:\/\/www.held.org.il\/blog\/?p=50"},"modified":"2008-02-18T23:08:02","modified_gmt":"2008-02-18T20:08:02","slug":"php-session-locks","status":"publish","type":"post","link":"http:\/\/www.held.org.il\/blog\/2008\/02\/php-session-locks\/","title":{"rendered":"PHP Session locks"},"content":{"rendered":"<p>At work we have a page that loads multiple PHP scripts (in parallel), using <a title=\"XMLHttpRequest from wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/XMLHttpRequest\">XMLHttpRequest<\/a>.<\/p>\n<p>Later, we noticed that if one PHP script responds very slowly (i.e. when we put sleep(120) inside), all the others are <strong>waiting<\/strong> till the first one finishes loading. In other words: the load is serial, not parallel!<\/p>\n<p><span style=\"color: #808080;\"><strong>What? Why?!<\/strong><\/span><br \/>\nA little research revealed an interesting phenomena: PHP session <strong>locks<\/strong> the session file till the session is closed. By default, the lock starts with <a title=\"session_write_close() from php.net\" href=\"http:\/\/www.php.net\/manual\/en\/function.session-write-close.php\"><em>session_start()<\/em><\/a> call, and ends at the end of the PHP script!<\/p>\n<p><!--more--><\/p>\n<p>So - loading multiple pages in parallel (of which all open the same session!), would result in first script locking the session and the others waiting for the release.. Then second lock, etc etc.<\/p>\n<p><span style=\"color: #808080;\"><strong>Behind the scenes:<\/strong><\/span><br \/>\n<em> session_start()<\/em> opens the session for writing with <em><a title=\"fopen() from php.net\" href=\"http:\/\/www.php.net\/fopen\">fopen(\"x\")<\/a> <\/em>- exclusive open. This takes care that a script would wait until the lock is released.<br \/>\n<strong><br \/>\n<span style=\"color: #808080;\"> A solution:<\/span><\/strong><br \/>\nIn many cases the session is needed for read only, thus there's no good reason for keeping the lock for the whole script..<br \/>\nSo for reading only, we can call <em>session_start()<\/em> to get the session vars, and immediately close it for writing, using <a title=\"session_write_close() from php.net\" href=\"http:\/\/www.php.net\/manual\/en\/function.session-write-close.php\"><em>session_write_close()<\/em><\/a>. And viola, $_SESSION vars stay!<\/p>\n<p>Hurray!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At work we have a page that loads multiple PHP scripts (in parallel), using XMLHttpRequest. Later, we noticed that if one PHP script responds very slowly (i.e. when we put sleep(120) inside), all the others are waiting till the first one finishes loading. In other words: the load is serial, not parallel! What? Why?! A [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[18,87,202],"_links":{"self":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":0,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.held.org.il\/blog\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}