Page 1 of 1

How to turn off implicit session locking in php?

Posted: Wed Mar 10, 2010 5:16 pm
by SteffenPoulsen
I would like to run to requests in parallel in the same session, i.e. have two instances of a php script run simultaneously.

To demonstrate, please see the code below (it will print 60 | chars with a second in between):

Next, simply open two tabs in your browser and call the script from both tabs.

If it is working as I hope, you will notice that the two tabs will not execute in parallel, but instead the script will start executing in one of the tabs, while the other will appear to hang. After the script in the first tabs finishes (after one minute), the next tab will start executing.

I have tried different approaches for achieving explicit locking, mainly using session_set_save_handler (http://theserverpages.com/php/manual/en ... andler.php), but to no effect - the locking is effective no matter what I come up with.

These are some pages that mention solutions for explicit locking, but none appear effective:

- http://thwartedefforts.org/2006/11/11/r ... -sessions/

- http://www.tonymarston.co.uk/php-mysql/ ... lones.html

- http://blog.perplexedlabs.com/2009/10/0 ... n-handler/

So, it appears to me that session_set_save_handler() is buggy - or am I just naively hoping it will work for me for a purpose it doesn't support?


Test script:
---------------

Code: Select all

 
<?php
print "[";
 
for($i = 0; $i < 100; $i++){
  $spaces.="<!-- bufferme -->";
} // for
 
//and then
 
for($i = 0; $i < 60; $i++){
  for($ii = 0; $ii < 200000; $ii++){
    //do something slow here
  } // for
  sleep(1);
  print "$spaces|";
  flush();
} // for
 
print "]";
?>
 
Expected result:
----------------
Both scripts running in parallel.

Actual result:
--------------
Scripts executing one at a time.

---

Further, I have had the suggestion of using session_write_close(); as the first php line in the script - but apparently this does not do any difference in allowing the second process to start executing.

Is there anyway to turn on explicit session locking in php?

Re: How to turn off implicit session locking in php?

Posted: Wed Mar 10, 2010 5:37 pm
by AbraCadaver
I think I have an answer, but I'm not sure.

#1 You contradict yourself. At the start of the post you say: "I would like to run to requests in parallel in the same session, i.e. have two instances of a php script run simultaneously." and "If it is working as I hope, you will notice that the two tabs will not execute in parallel, but instead the script will start executing in one of the tabs, while the other will appear to hang." and at the bottom of the post you say: "Expected result: Both scripts running in parallel. Actual result: Scripts executing one at a time."

#2 I don't see any use of sessions in your code.

So what is it that you want the script to do if opened in two separate tabs???

Re: How to turn off implicit session locking in php?

Posted: Wed Mar 10, 2010 6:12 pm
by SteffenPoulsen
Sorry - I only mean for the demonstration script to work as I hoped :-) - Demonstrating the problem, that is.

In this example I just want to avoid the use of sessions entirely - I want to know if there is a way to disable them locally for this script only, or (hopefully not :-)) that php will always enforce requests in the same session to be executed in serial?

Re: How to turn off implicit session locking in php?

Posted: Wed Mar 10, 2010 7:21 pm
by AbraCadaver
You're not using sessions in the example so you are avoiding using them. But to run the two scripts in serial you can use sessions. I haven't tested this, but the idea should work. This is only vaild for a session (i.e. this won't work for running the script in IE and also in FF, different sessions):

Code: Select all

set_time_limit(0);  //0 may cause problems if you have some bad or intensive code, maybe use 120?
session_start();
while(isset($_SESSION['running_instance']) && $_SESSION['running_instance'] == true) {}
$_SESSION['running_instance'] = true;
 
print "[";
 
for($i = 0; $i < 100; $i++){
  $spaces.="<!-- bufferme -->";
} // for
 
//and then
 
for($i = 0; $i < 60; $i++){
  for($ii = 0; $ii < 200000; $ii++){
    //do something slow here
  } // for
  sleep(1);
  print "$spaces|";
  flush();
} // for
print "]";
 
$_SESSION['running_instance'] = false;
The other alternative, especially if you run this CLI, is to use a lock file. Test for a lock file and if it doesn't exist create it and run the script, deleting it at the end. If it does exist, then wait (while loop) until it doesn't exist.

Re: How to turn off implicit session locking in php?

Posted: Thu Mar 11, 2010 12:20 am
by SteffenPoulsen
Thank you very much for your example, but it is the exact opposite behaviour that I'm after :-)

Actually, I won't even be able to test your example, because it seems there is no way I can get php to run two instances of a script simultaneously in the same session - it _always_ runs the scripts in the session in serial, no matter what kind of idea I seem to come up with.

So, I'm still very interested in the "key" that will let the two scripts execute concurrently in the same session - but yes, once I'm there we can start to discuss the bad sideeffects of work around the mutual exclusiveness php seem to enforce for now ... and so on :-)

Re: How to turn off implicit session locking in php?

Posted: Thu Mar 11, 2010 1:11 am
by VladSun
Probably it has something to do with this constraint: http://www.openajax.org/runtime/wiki/Th ... imit_Issue

Re: How to turn off implicit session locking in php?

Posted: Thu Mar 11, 2010 9:01 am
by SteffenPoulsen
Argh - apparently some browsers (including firefox) chooses to internally serialize requests against identical urls.

So, in effect, two requests, like this:

http://localhost/script.php and
http://localhost/script.php

Will always be serialized, the second request will simply never start in the browser before the first is finished.

However, two requests, like this:

http://localhost/script.php and
http://localhost/script.php?instance=2

will run happily in parallel.

Go figure - spent a few hours on this one :-)

- If anyone would happen to know how to disable this behaviour in firefox, please let me know!

Re: How to turn off implicit session locking in php?

Posted: Thu Mar 11, 2010 10:31 am
by AbraCadaver
Good information, thanks!

Re: How to turn off implicit session locking in php?

Posted: Sat Mar 13, 2010 8:41 pm
by PHPHorizons
Couldn't you just append a random number link?

Re: How to turn off implicit session locking in php?

Posted: Sun Mar 14, 2010 1:31 am
by Christopher
I think this thread looks like an example of heading it the wrong direction to solve a problem. I do that all the time. The initial problem was something about "session locking" whatever that is. When you get to the point where you are questioning whether PHP is buggy or not, you are probably doing something wrong.

All HTTP requests are essentially handled as if in parallel -- meaning that they are stateless and share nothing by design. The problem seems to be a desire to control caching and not setting HTTP headers correctly to achieve the desired result. You can append an ever changing parameter to your URLs, but setting HTTP headers is probably a better solution.