#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    11
    Rep Power
    0

    Question Single threaded shared function????


    This may sound odd but it is very important to me so I hope someone has an answer to help ease my mind.


    I need to create a function to perform a given task (doesn't matter what).

    The function will be accessible from several of my web pages.

    If multiple users hit my site at the same time and trigger this function, I need to be sure that only one will actually get to use it at a time. Is this possible? Is this the norm? Is there some special trick I need to use?

    Example:

    Bob, Sally and Tom all go to get_info.php at just about the same time. I want to make sure that the function do_my_work processes the request for only one user at a time, then the next and the next...

    Bob gets his info right away.
    Sally had to wait for Bob to get his info.
    Tom had to wait for Sally to get her info.


    I know that seems odd but the resource and task in question will simply implode on itself if two jobs overlap so I need to be sure that never happens.

  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,072
    Rep Power
    9398
    Possible? Yes. Norm? On average no. Special trick? Yes.

    There are good ways, decent ways, and last resort ways of doing this. Describe your server setup (operating system, shared vs. dedicated, who administers...) and the kind of work you're thinking of.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,466
    Rep Power
    594
    This would be very tricky because PHP is stateless. You might try to set up some kind of semaphore but there are a host of considerations. The main one being how to prevent a hang condition in the event that one of the clients never finishes after a lock. What will users see while waiting for their turn and how do you prevent the local browser from timing out.

    Off the top of my head, it might make more sense to do this using Ajax instead.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    11
    Rep Power
    0
    I figured out "A WAY" to do what I was after. Testing was a little tough until I realized Firefox was trying to be smarter than me.

    I set up several computers, all but one on the same network (one was my phone) and pointed them at my test page which was nothing but a hand full of IFRAME tags each pointing to the second test page that actually did the work.

    Of course, the natural state of PHP allowed all of the IFRAMES to fill very quickly even though the test function had a 5 second sleep command in it. Basically there was a single 5 second delay then all the IFRAMES populated because each copy of the function was running it its own world.

    Then I had this idea of using flock() to lock a file while I was doing my sleep and unlock it after.

    IT WORKED!

    Now each iframe loads one at a time in no particular order each with a 5 second delay with no overlap.

    The real function will not likely run for 5 seconds but that isn't the important part. The flock solved the problem. Here is the test code if you want to try it.

    PHP Code:
    <?
    $fp
    =fopen("/storage/my-lock-file.txt","w");
    if(
    flock($fp,LOCK_EX))
    {
         
    $s=time();
         
    sleep(5);
         
    $e=time();
         echo 
    "$s<br>$e";
    }
    else
    {
         echo 
    "no lock";
    }
    fclose($fp);
    ?>
    The HTML file is just a bunch of iframes...
    PHP Code:
    <iframe src="http://mysite.com/test.php" frameborder="1" height="70"></iframe><br>
    <
    iframe src="http://mysite.com/test.php" frameborder="1" height="70"></iframe><br>
    <
    iframe src="http://mysite.com/test.php" frameborder="1" height="70"></iframe><br>
    <
    iframe src="http://mysite.com/test.php" frameborder="1" height="70"></iframe><br>
    <
    iframe src="http://mysite.com/test.php" frameborder="1" height="70"></iframe><br>
    <
    iframe src="http://mysite.com/test.php" frameborder="1" height="70"></iframe><br
    Try it with and without the flock to see the difference.

    If anyone has a better method, I would love to see it. This one will do the trick for me but I am always up for better solutions.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    11
    Rep Power
    0
    Originally Posted by requinix
    Possible? Yes. Norm? On average no. Special trick? Yes.
    Besides the file locking trick that I just figured out, did you have something else in mind?
  10. #6
  11. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,072
    Rep Power
    9398
    Originally Posted by schworak
    Besides the file locking trick that I just figured out, did you have something else in mind?
    Depends on your setup. Queues, daemons, file locks, semaphores, shared memory, for a few ideas.

IMN logo majestic logo threadwatch logo seochat tools logo