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. :confused:

[B]<?php
include(‘connectdb.php’);

some mysql queries…

sleep(10);

some other mysql queries…

mysql_close($connection);

?>[/B]

this comment may be of some help.

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

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']==0 && $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']==0 && $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']==0 && $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); 
  
 }

?>

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 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.

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.

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 !!