Skip to content

Instantly share code, notes, and snippets.

@attozk
Last active August 29, 2015 14:07
Show Gist options
  • Save attozk/5ddf1e4acb6f76f24478 to your computer and use it in GitHub Desktop.
Save attozk/5ddf1e4acb6f76f24478 to your computer and use it in GitHub Desktop.
MYSQLND MYSQLI_ASYNC pretty much useless for single connection
############################################################
###
### Useless for a single mysql connection trying to
### ASYNC multiple db calls
###
$link1 = mysqli_connect("localhost","user","pwd","dbname");
$s1 = "SELECT SLEEP(10)";
$s2 = "SELECT SLEEP(5)";
$s3 = "Select SLEEP(0)";
mysqli_query($link1, $s1, MYSQLI_ASYNC);
mysqli_query($link1, $s2, MYSQLI_ASYNC);
mysqli_query($link1, $s3, MYSQLI_ASYNC);
$started=time();
for ($x=0; $x<5; $x++)
{
$ready = $reject = $errors = array($link1, $link1, $link1);
print "\niteration $x at t+" . (time()-$started) . "\n";
mysqli_poll($ready, $errors, $reject, 1);
print "ready = " . count($ready) . "\n";
foreach($ready as $r) {
$result = mysqli_reap_async_query($r);
if(is_object($result))
{
print_r($result->fetch_array(MYSQLI_ASSOC));
mysqli_free_result($result);
}
};
sleep(4);
}
## Will output:
iteration 0 at t+0
ready = 0
iteration 1 at t+5
ready = 0
iteration 2 at t+10
ready = 3
Array
(
[SLEEP(10)] => 0
)
iteration 3 at t+14
ready = 0
iteration 4 at t+19
ready = 0
############################################################
###
### Works for multiple mysql connections
###
$link1 = mysqli_connect("localhost","user","pwd","dbname");
$link2 = mysqli_connect("localhost","user","pwd","dbname");
$link3 = mysqli_connect("localhost","user","pwd","dbname");
$s1 = "SELECT SLEEP(10)";
$s2 = "SELECT SLEEP(5)";
$s3 = "Select SLEEP(0)";
mysqli_query($link1, $s1, MYSQLI_ASYNC);
mysqli_query($link2, $s2, MYSQLI_ASYNC);
mysqli_query($link3, $s3, MYSQLI_ASYNC);
$started=time();
for ($x=0; $x<5; $x++)
{
$ready = $reject = $errors = array($link1, $link2, $link3);
print "\niteration $x at t+" . (time()-$started) . "\n";
mysqli_poll($ready, $errors, $reject, 1);
print "ready = " . count($ready) . "\n";
foreach($ready as $r) {
$result = mysqli_reap_async_query($r);
if(is_object($result))
{
print_r($result->fetch_array(MYSQLI_ASSOC));
mysqli_free_result($result);
}
};
sleep(4);
}
## will output
iteration 0 at t+0
ready = 1
Array
(
[SLEEP(0)] => 0
)
iteration 1 at t+4
ready = 1
Array
(
[SLEEP(5)] => 0
)
iteration 2 at t+9
ready = 1
Array
(
[SLEEP(10)] => 0
)
iteration 3 at t+14
ready = 0
iteration 4 at t+19
ready = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment