How to turn off implicit session locking in php?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
SteffenPoulsen
Forum Newbie
Posts: 4
Joined: Wed Mar 10, 2010 5:09 pm

How to turn off implicit session locking in php?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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???
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
SteffenPoulsen
Forum Newbie
Posts: 4
Joined: Wed Mar 10, 2010 5:09 pm

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

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
SteffenPoulsen
Forum Newbie
Posts: 4
Joined: Wed Mar 10, 2010 5:09 pm

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

Post 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 :-)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

Probably it has something to do with this constraint: http://www.openajax.org/runtime/wiki/Th ... imit_Issue
There are 10 types of people in this world, those who understand binary and those who don't
SteffenPoulsen
Forum Newbie
Posts: 4
Joined: Wed Mar 10, 2010 5:09 pm

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

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Good information, thanks!
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

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

Post by PHPHorizons »

Couldn't you just append a random number link?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply