SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a script paused with sleep, same script requested by another user. what happens?

    hello,

    i was wondering about an apache reaction with the sleep() function, thus my posting in this section.


    it's a theoretical question (I say that to avoid the casual "why you wanna do it anyway?" post).

    I have a script. At the end of the script, I want to wait one minute before outputing anything:
    PHP Code:
    //code
    sleep(60)
    //echo('some output'); 
    what happens if the script is requested by a user and that another user requests the same page? can apache run the two scripts at the same time and let them sleep both for a minute or do you have to wait for a script to be fully executed before it get used by another user?

    Same question with variables. The code before the sleep(60) part would treat different variables based on user input. Will it be a problem if a user submits a form with his own input while the script is already sleeping with the variables values of another user who would already have had submitted his form without the result being outputed because of the delay caused by sleep()? In other words: can two scripts be requested at the exact same time and keep store the input of two different users or will the second user erase the values entered by the first user.

    i know it sounds awkward... but i hope i'll get some answers anyway

  2. #2
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Apache is multi - forking, meaning that it can handle many request simultaneously. So for each request, a completely seperate version of yoru script is run in memory.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    I don't think it would make a greeat deal of difference to the scripts as if it did only one person could view your website at any one time...

    It would however put an enormous strain on your server. Read the comments at the bottom of the php.net documentation. www.php.net/sleep for more information.

    The same is for variables (except the server strain that is!). If variables could only process one request at a time, your traffic would be non existant!

    Cheers

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your replies

    Quote Originally Posted by spikeZ
    It would however put an enormous strain on your server. Read the comments at the bottom of the php.net documentation. www.php.net/sleep for more information.
    Are you sure? I got some other info in this thread.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    sorry, I should have clarified. The problem arises with usleep on a windows box.
    From php.net

    Should be noted that functions that loop really fast to create a delay also consume 100% CPU while doing the loop. Try creating a dummy loop that goes 100000 times, watch it choke your machine. If you really need usleep() don't use windows.

    My bad, should have been clearer

    Mike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm writing an application that uses a similar process. I've requested two times this script for testing purposes:
    PHP Code:
    for ($i 1$i <= 10$i++) {

    $sql="insert into foo set bar = $i";

    @
    mysql_query($sql);

    sleep(120); 


    my website didn't seem to suffer neither my traffic stats to be unhappy.

  7. #7
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cross post.

    i've just added a few more requests to see how my server did react and it seems pretty fine...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •