SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php sleep() problems.

    If i run php script shown below in two different tabs from same browser with time lag (for example 2 sec), the script in second tab waits until the script in first tab finishes and takes total of 12 secs to complete. Is there any way in php so that the same script can run simultaneously without blocking and accessing database.


    <?php
    include('connectdb.php');

    some mysql queries...........

    sleep(10);

    some other mysql queries..........


    mysql_close($connection);

    ?>

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this comment may be of some help.
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you're using sessions even though you've not shown your code (which isn't very useful).

    You may not think that showing us all of your code is relevant but it is.

    Sessions are only available to one script at a time. If you have multiple scripts trying to access it while one is sleeping the others will have to wait.

    session_write_close() does the job of giving up session access but once done I don't think you can use session_start again in that same script.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  4. #4
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tangoforce View Post
    Sounds like you're using sessions even though you've not shown your code (which isn't very useful).

    You may not think that showing us all of your code is relevant but it is.

    Sessions are only available to one script at a time. If you have multiple scripts trying to access it while one is sleeping the others will have to wait.

    session_write_close() does the job of giving up session access but once done I don't think you can use session_start again in that same script.
    PHP Code:
    <?php

    include('connectdb.php');     // connection to database

    $info=mysql_query("SELECT a,b,c,d,e from info where a=1 LIMIT 0,1") or die('couldnt select user info');
    $num=mysql_num_rows($info);

    if(
    $num==0) {;}                                                                       
    else if(
    $num==1)
     {
      
    $info=mysql_fetch_array($info,MYSQL_ASSOC) or die('couldnt get user info');
      if(
    $info['e']=='on')
       {
        
    mysql_query("INSERT INTO {$info['d']}(a,b,c) VALUES({$info['a']},{$info['b']},'{$info['c']}')") or   die("failed insert into d table query!");
        
        
    $user2=mysql_query("SELECT a from {$info['d']} where a!={$info['a']} AND c!='{$info['c']}' AND (b={$info['b']}+1 || b={$info['b']}-1 || b={$info['b']}) AND engb=0  LIMIT 0,1") or die('couldnt select user2 a');
        
    $num1=mysql_num_rows($user2);
        
    $row1=mysql_fetch_array($user2,MYSQL_ASSOC);
         


        
    $user=mysql_query("SELECT engb from {$info['d']} where a={$info['a']}") or die('couldnt select user engb');
        
    $row2=mysql_fetch_array($user,MYSQL_ASSOC) or die('couldnt get user engb');
            
        if(
    $row2['engb']==&& $num1!=0
        {
         
    mysql_query("UPDATE {$info['d']} SET engb={$info['a']} WHERE a={$row1['a']}")  or die("user2 update query failed");
         
    mysql_query("UPDATE {$info['d']} SET engb={$row1['a']} WHERE a={$info['a']}")  or die("user update query failed");
     
        
       

        }
        else 
        {
         
         
    sleep(20);                                                                  // SLEEP
         
    $user=mysql_query("SELECT engb from {$info['d']} where a={$info['a']}") or die('couldnt select user engb');
         
    $row2=mysql_fetch_array($user,MYSQL_ASSOC) or die('couldnt get user engb');
        
         if(
    $row2['engb']!=0) {mysql_close($connection);exit();}
         else                                                                           
    // SHIFT TO GLOBAL
           
    { echo "sleep over";
            
    mysql_query("DELETE FROM {$info['d']}  WHERE a={$info['a']}");
            
    mysql_query("INSERT INTO global(a) VALUES({$info['a']})") or die("failed insert into global table query!");

            
    $user2=mysql_query("SELECT a from global where a!={$info['a']} AND engb=0 LIMIT 0,1") or die('couldnt select user2 a');
            
    $num1=mysql_num_rows($user2);
            
    $row1=mysql_fetch_array($user2,MYSQL_ASSOC);

            
    $user=mysql_query("SELECT engb from global where a={$info['a']}") or die('couldnt select user engb');
            
    $row2=mysql_fetch_array($user,MYSQL_ASSOC) or die('couldnt get user engb');

            if(
    $row2['engb']==&& $num1!=0
             {
              
    mysql_query("UPDATE global SET engb={$info['a']} WHERE a={$row1['a']}") or die("user2 update query failed");
              
    mysql_query("UPDATE global SET engb={$row1['a']} WHERE a={$info['a']}") or die("user update query failed");
     
             
       

             }

           }                     
        }   
        
       }

    else
       {
        
    mysql_query("INSERT INTO global(a) VALUES({$info['a']})") or die("failed insert into global table query!");

        
    $user2=mysql_query("SELECT a from global where a!={$info['a']} AND engb=0  LIMIT 0,1") or die('couldnt select user2 a');
        
    $num1=mysql_num_rows($user2);
        
    $row1=mysql_fetch_array($user2,MYSQL_ASSOC);

        
    $user=mysql_query("SELECT engb from global where a={$info['a']}") or die('couldnt select user engb');
        
    $row2=mysql_fetch_array($user,MYSQL_ASSOC) or die('couldnt get user engb');

        if(
    $row2['engb']==&& $num1!=0
         {
          
    mysql_query("UPDATE global SET engb={$info['a']} WHERE a={$row1['a']}") or die("user2 update query failed");
          
    mysql_query("UPDATE global SET engb={$row1['a']} WHERE a={$info['a']}") or die("user update query failed");
     
          

         }
       }


    mysql_close($connection); 
      
     }

    ?>

  5. #5
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats not even the same code. In that code you use sleep(20) yet above you've quoted code that uses sleep(10).

    You're not being consistent with what you're telling us.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  6. #6
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand what the point of putting a 20 second delay on that script is - could you please clarify?
    aaron-fisher.com - PHP articles and more

  7. #7
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aamonkey View Post
    I don't understand what the point of putting a 20 second delay on that script is - could you please clarify?
    hi,
    Above script is trying to find a suitable match(user1) for user based on a,b,c,d,e properties.Script will wait for 20 secs to find suitable match , if not it will select a random match.
    thanks.

  8. #8
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You realize that when you call sleep it halts all script execution and nothing happens?
    aaron-fisher.com - PHP articles and more

  9. #9
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aamonkey View Post
    You realize that when you call sleep it halts all script execution and nothing happens?
    hi,

    Actually the database is dynamic and users will be adding constantly.Thatz why the script waits for 20 sec and check database again for new users with close a,b,c,d,e and try to match them.That means the script go through the database twice for appropriate match with span of 20sec.Even after 20sec ,if there is no perfect match the script will match a random person irrespective of a,b,c,d,e.

  10. #10
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ravi1991 View Post
    hi,

    Actually the database is dynamic and users will be adding constantly.Thatz why the script waits for 20 sec and check database again for new users with close a,b,c,d,e and try to match them.That means the script go through the database twice for appropriate match with span of 20sec.Even after 20sec ,if there is no perfect match the script will match a random person irrespective of a,b,c,d,e.

    But why. What are the chances of the properties your searching for being entered with 20 seconds of the first query. Very small I'd think !!
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


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
  •