Beispiel #1
1
 public function execute()
 {
     $collect = array();
     $link_count = count($this->links);
     $processed = 0;
     do {
         $links = $errors = $reject = array();
         foreach ($this->links as $link) {
             $links[] = $errors[] = $reject[] = $link;
         }
         if (!mysqli_poll($links, $errors, $reject, 0, 1000)) {
             continue;
         }
         for ($i = 0; $i < $link_count; $i++) {
             $link = $this->links[$i];
             if (mysqli_errno($link)) {
                 throw new \RuntimeException(mysqli_error($link), mysqli_errno($link));
             }
             if ($result = $link->reap_async_query()) {
                 if (is_object($result)) {
                     $temp = array();
                     while (($row = $result->fetch_assoc()) && ($temp[] = $row)) {
                     }
                     $collect[$i] = $temp;
                     mysqli_free_result($result);
                 } else {
                     $collect[$i] = $result;
                 }
             }
             $processed++;
         }
     } while ($processed < $link_count);
     return $collect;
 }
Beispiel #2
0
 /** Execute query or get its data
  * @param string query identifier
  * @param array array(string $query, [array $credentials]) for executing query, array() for getting data
  * @return bool|mysqli_result
  */
 function __call($name, array $args)
 {
     if ($args) {
         // execute query
         $query = $args[0];
         $credentials = $this->credentials;
         if (isset($args[1])) {
             $credentials = $args[1] + $credentials;
         }
         $connection = call_user_func_array('mysqli_connect', $credentials);
         $this->connections[$name] = $connection;
         return $connection->query($query, MYSQLI_ASYNC);
     }
     // get data
     if (!isset($this->connections[$name])) {
         // wrong identifier
         return false;
     }
     //! handle second call with the same $name
     $connection = $this->connections[$name];
     do {
         $links = $errors = $reject = $this->connections;
         mysqli_poll($links, $errors, $reject, $this->timeout);
     } while (!in_array($connection, $links, true) && !in_array($connection, $errors, true) && !in_array($connection, $reject, true));
     return $connection->reap_async_query();
 }
function poll_async($offset, $link, $links, $errors, $reject, $exp_ready, $use_oo_syntax)
{
    if ($exp_ready !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 1000))) {
        printf("[%03d + 1] There should be %d links ready to read from, %d ready\n", $offset, $exp_ready, $tmp);
    }
    foreach ($links as $mysqli) {
        if ($use_oo_syntax) {
            $res = $mysqli->reap_async_query();
        } else {
            $res = mysqli_reap_async_query($mysqli);
        }
        if (is_object($res)) {
            printf("[%03d + 2] %s\n", $offset, var_export($res->fetch_assoc(), true));
        } else {
            if (mysqli_errno($mysqli) > 0) {
                printf("[%03d + 3] Error indicated through links array: %d/%s", $offset, mysqli_errno($mysqli), mysqli_error($mysqli));
            } else {
                printf("[%03d + 4] Cannot fetch and no error set - non resultset query (no SELECT)!\n", $offset);
            }
        }
    }
    foreach ($errors as $mysqli) {
        printf("[%03d + 5] Error on %d: %d/%s\n", $offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
    }
    foreach ($reject as $mysqli) {
        printf("[%03d + 6] Rejecting thread %d: %d/%s\n", $offset, mysqli_thread_id($mysqli), mysqli_errno($mysqli), mysqli_error($mysqli));
    }
}
Beispiel #4
0
 /**
  * @param string
  * @return \React\Promise\PromiseInterface
  */
 function query($query)
 {
     return $this->pool->getConnection()->then(function (mysqli $conn) use($query) {
         $status = $conn->query($query, MYSQLI_ASYNC);
         if ($status === false) {
             $this->pool->free($conn);
             throw new Exception($conn->error);
         }
         $id = spl_object_hash($conn);
         $this->conn[$id] = $conn;
         $this->query[$id] = $query;
         $this->deferred[$id] = $deferred = new Deferred();
         if (!isset($this->timer)) {
             $this->timer = $this->loop->addPeriodicTimer(0.01, function () {
                 $links = $errors = $reject = $this->conn;
                 mysqli_poll($links, $errors, $reject, 0);
                 // don't wait, just check
                 $each = array('links' => $links, 'errors' => $errors, 'reject' => $reject);
                 foreach ($each as $type => $connects) {
                     /**
                      * @var $conn mysqli
                      */
                     foreach ($connects as $conn) {
                         $id = spl_object_hash($conn);
                         if (isset($this->conn[$id])) {
                             $deferred = $this->deferred[$id];
                             if ($type == 'links') {
                                 /**
                                  * @var $result mysqli_result
                                  */
                                 $result = $conn->reap_async_query();
                                 if ($result === false) {
                                     $deferred->reject(new Exception($conn->error . '; sql: ' . $this->query[$id]));
                                 } else {
                                     $deferred->resolve(new Result($result, $conn->insert_id, $conn->affected_rows));
                                 }
                             }
                             if ($type == 'errors') {
                                 $deferred->reject(new Exception($conn->error . '; sql: ' . $this->query[$id]));
                             }
                             if ($type == 'reject') {
                                 $deferred->reject(new Exception('Query was rejected; sql: ' . $this->query[$id]));
                             }
                             unset($this->deferred[$id], $this->conn[$id], $this->query[$id]);
                             $this->pool->free($conn);
                         }
                     }
                 }
                 if (empty($this->conn)) {
                     $this->timer->cancel();
                     $this->timer = null;
                 }
             });
         }
         return $deferred->promise();
     });
 }
Beispiel #5
0
 /**
  * @inheritDoc
  */
 public function poll(array $links)
 {
     $read = $error = $reject = $links;
     if (false === mysqli_poll($read, $error, $reject, $this->timeout)) {
         throw new MysqliException(sprintf('Maximum polling time of %d seconds exceeded.', $this->timeout));
     }
     foreach ($reject as $link) {
         /* @var $link \mysqli */
         throw new MysqliException(sprintf('The connection to the host %s is rejected.', $link->host_info));
     }
     return [$read, $error];
 }
Beispiel #6
0
 function wait($timeout = 1.0)
 {
     $_timeout_sec = intval($timeout);
     $_timeout_usec = intval(($timeout - $_timeout_sec) * 1000 * 1000);
     $taskSet = $this->list;
     $processed = 0;
     do {
         $links = $errors = $reject = array();
         foreach ($taskSet as $k => $retObj) {
             $links[] = $errors[] = $reject[] = $retObj->db;
         }
         //wait mysql server response
         if (!mysqli_poll($links, $errors, $reject, $_timeout_sec, $_timeout_usec)) {
             continue;
         }
         /**
          * @var $link mysqli
          */
         foreach ($links as $link) {
             $_retObj = $this->list[$link->_co_id];
             $result = $link->reap_async_query();
             if ($result) {
                 if (is_object($result)) {
                     $_retObj->result = new MySQLiRecord($result);
                     if ($_retObj->callback) {
                         call_user_func($_retObj->callback, $_retObj->result);
                     }
                     $_retObj->code = 0;
                 } else {
                     $_retObj->code = CoMySQLResult::ERR_NO_OBJECT;
                 }
             } else {
                 trigger_error(sprintf("MySQLi Error: %s", $link->error));
                 $_retObj->code = $link->errno;
             }
             //从任务队列中移除
             unset($taskSet[$link->_co_id]);
             $processed++;
         }
     } while ($processed < count($this->list));
     //将连接重新放回池中
     foreach ($this->list as $_retObj) {
         $this->pool[] = $_retObj->db;
     }
     //初始化数据
     $this->list = array();
     $this->sqlIndex = 0;
     return $processed;
 }
 function fetch()
 {
     for ($i = 1; $i <= 5; $i++) {
         $read = $errors = $reject = self::$links;
         $re = mysqli_poll($read, $errors, $reject, 1);
         foreach ($read as $obj) {
             if ($this->obj === $obj) {
                 $sql_result = $obj->reap_async_query();
                 $sql_result_array = $sql_result->fetch_array(MYSQLI_ASSOC);
                 //只有一行
                 $sql_result->free();
                 return $sql_result_array;
             }
         }
     }
 }
Beispiel #8
0
 public function loopTick(TimerInterface $timer)
 {
     if (count($this->conns) == 0) {
         // If we are shutting down, and have nothing to check, kill the timer.
         if ($this->shuttingDown) {
             // TODO: Possible race condition if shutdown also queues queries, such as a final save.
             // This could be prematurely cancelled.
             $timer->cancel();
         }
         // Nothing in the queue.
         return;
     }
     $reads = $errors = $rejects = [];
     foreach ($this->conns as $conn) {
         $reads[] = $conn['mysqli'];
     }
     // Returns immediately, the non-blocking magic!
     if (mysqli_poll($reads, $errors, $rejects, 0) < 1) {
         return;
     }
     /** @var Connection $read */
     foreach ($reads as $read) {
         /** @var Deferred $deferred */
         $deferred = $this->conns[$read->id]['deferred'];
         $result = $read->reap_async_query();
         if ($result !== false) {
             $deferred->resolve($result);
             // $result is true for non-select queries.
             if ($result instanceof \mysqli_result) {
                 // If userland code has already freed the result, this will throw a warning.
                 // No need to throw a warning here...
                 // If you know how to check if the result has already been freed, please PR!
                 @$result->free();
             }
         } else {
             $deferred->reject($read->error);
         }
         // Release the connection
         $this->pool->releaseConnection($read);
         unset($this->conns[$read->id]);
     }
     // Check error pile.
     // Current understanding is that this would only happen if the connection
     // was closed, or not opened correctly.
     foreach ($errors as $error) {
         $this->pool->releaseConnection($error);
         unset($this->conns[$error->id]);
         throw new \Exception('Unexpected mysqli_poll $error.');
     }
     // Check rejection pile.
     // Current understanding is that this would only happen if we passed a
     // connection that was already reaped. But... maybe not.
     foreach ($rejects as $reject) {
         $this->pool->releaseConnection($reject);
         unset($this->conns[$reject->id]);
         throw new \Exception('Unexpected mysqli_poll $reject.');
     }
     // Duplicated check to avoid one extra tick!
     // If we are shutting down, cancel timer once connections finish.
     if ($this->shuttingDown && count($this->conns) == 0) {
         $timer->cancel();
     }
 }
Beispiel #9
0
 public function wait()
 {
     // событийный цикл,
     $processed = 0;
     $res = [];
     do {
         $links = $errors = $reject = [];
         foreach ($this->_pool as $mysql) {
             $links[] = $errors[] = $reject[] = $mysql;
         }
         # опрашиваем все коннекции на наличие ответа
         if (!mysqli_poll($links, $errors, $reject, 60)) {
             continue;
         }
         foreach ($links as $k => $link) {
             # получаем ответ из асинхронного  запроса
             if ($result = $link->reap_async_query()) {
                 if ($result && $result instanceof \mysqli_result) {
                     $res[$k] = $result->fetch_row();
                     # Handle returned result
                     mysqli_free_result($result);
                 }
             } else {
                 throw new Exception(sprintf("MySQLi Error: %s", mysqli_error($link)));
             }
             $processed++;
         }
     } while ($processed < count($this->_pool));
     return $res;
 }
Beispiel #10
0
<?php

require 'connect.inc';
$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
mysqli_close($link);
$read = $error = $reject = array();
$read[] = $error[] = $reject[] = $link;
mysqli_poll($read, $error, $reject, 1);
echo "okey";
    $links[mysqli_thread_id($link)] = array('query' => $queries[$i], 'link' => $link, 'processed' => false);
}
$saved_errors = array();
do {
    $poll_links = $poll_errors = $poll_reject = array();
    foreach ($links as $thread_id => $link) {
        if (!$link['processed']) {
            $poll_links[] = $link['link'];
            $poll_errors[] = $link['link'];
            $poll_reject[] = $link['link'];
        }
    }
    if (0 == count($poll_links)) {
        break;
    }
    if (0 == ($num_ready = mysqli_poll($poll_links, $poll_errors, $poll_reject, 0, 200000))) {
        continue;
    }
    if (!empty($poll_errors)) {
        die(var_dumtest_mysqli_poll_mixing_insert_select_procedure_1($poll_errors));
    }
    foreach ($poll_links as $link) {
        $thread_id = mysqli_thread_id($link);
        $links[$thread_id]['processed'] = true;
        if (is_object($res = mysqli_reap_async_query($link))) {
            // result set object
            while ($row = mysqli_fetch_assoc($res)) {
                // eat up all results
            }
            mysqli_free_result($res);
        } else {
 /**
  * 获取所有数据.
  *
  * @return array
  * @throws \Exception 查询失败.
  */
 public static function asyncFetchAll()
 {
     $allLinks = self::$asyncConnections;
     $asyncData = array();
     $processed = 0;
     do {
         $links = $errors = $reject = array();
         foreach ($allLinks as $link) {
             $links[] = $errors[] = $reject[] = $link;
         }
         if (!mysqli_poll($links, $errors, $reject, 1)) {
             continue;
         }
         foreach ($links as $link) {
             if ($result = $link->reap_async_query()) {
                 $asyncData = array_merge($asyncData, $result->fetch_all(MYSQLI_ASSOC));
                 $result->free();
             } else {
                 throw new \Exception("sync query failed!");
             }
             $processed++;
         }
     } while ($processed < count($allLinks));
     static::clearAsyncConnections();
     return $asyncData;
 }
Beispiel #13
0
<?php

error_reporting(E_ALL);
$tablica = array();
$test1 = mysqli_poll($test2, $test3, $tablica, null);
$test2 = array();
$test2 = array();
$test1 = mysqli_poll($test2, $test3, $tablica, null);
echo "okey";
Beispiel #14
0
 /**
  * Sends a query to the database server
  *
  * @param string  the SQL query string
  *
  * @return mixed  + a PHP result resrouce for successful SELECT queries
  *                + the DB_OK constant for other successful queries
  *                + a DB_Error object on failure
  */
 function simpleQuery($query)
 {
     $ismanip = $this->_checkManip($query);
     $this->last_query = $query;
     $query = $this->modifyQuery($query);
     if ($this->_db) {
         if (!@mysqli_select_db($this->connection, $this->_db)) {
             return $this->mysqliRaiseError(DB_ERROR_NODBSELECTED);
         }
     }
     if (!$this->autocommit && $ismanip) {
         if ($this->transaction_opcount == 0) {
             $result = @mysqli_query($this->connection, 'SET AUTOCOMMIT=0');
             $result = @mysqli_query($this->connection, 'BEGIN');
             if (!$result) {
                 return $this->mysqliRaiseError();
             }
         }
         $this->transaction_opcount++;
     }
     if (empty($this->features['abort'])) {
         $result = @mysqli_query($this->connection, $query);
         if (!$result) {
             return $this->mysqliRaiseError();
         }
         if (is_object($result)) {
             return $result;
         }
         return DB_OK;
     }
     $result = @mysqli_query($this->connection, $query, MYSQLI_ASYNC);
     $thread_id = $this->connection->thread_id;
     $isRunning = session_status() == PHP_SESSION_ACTIVE;
     if (!$isRunning) {
         @session_start();
     }
     if (!isset($_SESSION['MYSQLI_THREAD_ID'])) {
         $_SESSION['MYSQLI_THREAD_ID'] = array();
     }
     $_SESSION['MYSQLI_THREAD_ID'][] = $thread_id;
     @session_write_close();
     do {
         $links = $errors = $reject = array($this->connection);
         $poll = mysqli_poll($links, $errors, $reject, 0, 500000);
     } while (!$poll);
     @session_start();
     if (!in_array($thread_id, $_SESSION['MYSQLI_THREAD_ID'])) {
         return $this->mysqliRaiseError(DB_ERROR_CONNECT_FAILED);
     }
     unset($_SESSION['MYSQLI_THREAD_ID'][array_search($thread_id, $_SESSION['MYSQLI_THREAD_ID'])]);
     if (!$isRunning) {
         @session_write_close();
     }
     $result = $this->connection->reap_async_query();
     if (!$result) {
         return $this->mysqliRaiseError();
     }
     if (is_object($result)) {
         return $result;
     }
     return DB_OK;
 }
 /**
  * Works Brilliantly
  */
 public function disabled_testTheConcept()
 {
     echo PHP_EOL;
     $pool = [];
     for ($i = 0; $i < 0; $i++) {
         $mysqli = $this->getNewMysqliConnection();
         $mysqli_id = $mysqli->thread_id;
         $mysqli_my = $mysqli->countId;
         $sql = 'SELECT SLEEP(' . $i . ');';
         $mysqli->query($sql, MYSQLI_ASYNC);
         $deferred = new Deferred();
         $pool[$mysqli->thread_id] = ['mysqli' => $mysqli, 'deferred' => $deferred];
         $deferred->promise()->then(function (\mysqli_result $result) use(&$rowCount) {
             $rows = $result->fetch_all(MYSQLI_ASSOC);
             $rowCount = count($rows);
             echo $rowCount;
         });
     }
     for ($i = 0; $i < 3; $i++) {
         $mysqli = $this->getNewMysqliConnection();
         $mysqli_id = $mysqli->thread_id;
         $sql = 'SELECT foo FROM';
         $mysqli->query($sql, MYSQLI_ASYNC);
         $deferred = new Deferred();
         $pool[$mysqli->thread_id] = ['mysqli' => $mysqli, 'deferred' => $deferred];
         $deferred->promise()->then(function (\mysqli_result $result) use(&$rowCount) {
             echo 'M';
             $rows = $result->fetch_all(MYSQLI_ASSOC);
             $rowCount = count($rows);
             echo $rowCount;
             $result->close();
         });
     }
     for ($i = 0; $i < 6; $i++) {
         $mysqli = $this->getNewMysqliConnection();
         $mysqli_id = $mysqli->thread_id;
         $sql = 'SELECT * FROM simple_table WHERE id = ' . $i;
         $mysqli->query($sql, MYSQLI_ASYNC);
         $deferred = new Deferred();
         $pool[$mysqli->thread_id] = ['mysqli' => $mysqli, 'deferred' => $deferred];
         $deferred->promise()->then(function (\mysqli_result $result) use(&$rowCount) {
             $rows = $result->fetch_all(MYSQLI_ASSOC);
             $rowCount = count($rows);
             echo $rowCount;
             $result->close();
         });
     }
     $loop = Factory::create();
     $loop->addPeriodicTimer(0.01, function ($timer) use(&$pool) {
         $reads = [];
         foreach ($pool as $p) {
             $reads[] = $p['mysqli'];
         }
         if (count($reads) < 1) {
             return;
         }
         if (mysqli_poll($reads, $errors = [], $rejects = [], 0) < 1) {
             return;
         }
         echo '(' . count($reads) . '/' . count($errors) . '/' . count($rejects) . ')';
         /** @var \mysqli $read */
         foreach ($reads as $read) {
             //echo '{'.$read->thread_id.'}';
             $deferred = $pool[$read->thread_id]['deferred'];
             $result = $read->reap_async_query();
             if ($result === false) {
                 echo 'W';
             }
             $deferred->resolve($result);
             unset($pool[$read->thread_id]);
         }
         foreach ($errors as $error) {
             echo 'A';
             unset($pool[$error->thread_id]);
         }
         foreach ($rejects as $reject) {
             echo 'B';
             unset($pool[$reject->thread_id]);
         }
         if (count($pool) == 0) {
             $timer->cancel();
         }
     });
     $loop->run();
     //$this->assertEquals(1, $rowCount);
     //$this->assertEquals($mysqli_id, $mysqli->thread_id);
 }
Beispiel #16
0
 function query($sql, $options = [])
 {
     $timeout = isset($options['timeout']) ? $options['timeout'] : $_ENV['config']['timeout']['mysql_read'];
     if (!$this->is_avaliable()) {
         return cy_dt(CYE_DB_CONNECT_ERROR, 'mysql connect is not available.');
     }
     $this->db->query($sql, MYSQLI_ASYNC);
     $finished = 0;
     $t1 = microtime(true);
     while (!$finished) {
         $links = $errors = $reject = [$this->db];
         if (!mysqli_poll($links, $errors, $reject, 0, 100000)) {
             if (microtime(true) - $t1 < $timeout) {
                 continue;
             }
             $this->db->close();
             cy_log(CYE_ERROR, 'mysql query timeout, over %f second', $timeout);
             $dt = cy_dt(CYE_NET_TIMEOUT, 'mysql query timeout');
             goto end;
         }
         $finished = 1;
     }
     if (!($result = $this->db->reap_async_query())) {
         cy_log(CYE_ERROR, 'mysql query error [%d] [%s]', $this->db->errno, $this->db->error);
         $dt = cy_dt($this->db->errno, $this->db->error);
         goto end;
     }
     /* For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object */
     if (is_object($result)) {
         $rows = $result->fetch_all(MYSQLI_ASSOC);
         mysqli_free_result($result);
         $dt = cy_dt(0, $rows);
         goto end;
     }
     /* For other successful queries mysqli_query() will return TRUE. */
     $r = ['affected_rows' => $this->db->affected_rows, 'insert_id' => $this->db->insert_id];
     $dt = ['errno' => 0, 'data' => $r, 'info' => $this->db->info];
     end:
     $t2 = microtime(true);
     cy_stat('mysql-query', ($t2 - $t1) * 1000000);
     return $dt;
 }
Beispiel #17
0
echo $s . "\n";
$link1 = mysqli_connect('172.16.2.111', 'test', 'xxxx', 'test');
$link2 = mysqli_connect('172.16.2.112', 'test', 'xxxx', 'test');
//mysqli_query("SET NAMES utf8");
mysqli_set_charset($link1, 'utf8');
mysqli_set_charset($link2, 'utf8');
$link1->query("select * from goods", MYSQLI_ASYNC);
$link2->query("select * from goods", MYSQLI_ASYNC);
$all_links = array($link1, $link2);
$processed = 0;
do {
    $links = $errors = $reject = array();
    foreach ($all_links as $link) {
        $links[] = $errors[] = $reject[] = $link;
    }
    if (!mysqli_poll($links, $errors, $reject, 1)) {
        continue;
    }
    // $i=0;
    $rows = array();
    foreach ($links as $link) {
        if ($result = $link->reap_async_query()) {
            //print_r($result->fetch_row());
            while ($row = $result->fetch_row()) {
                //print_r($row);
                // $rows[$i][] = $row;
                $rows[] = $row;
            }
            echo count($rows[$i]) . "\n";
            if (is_object($result)) {
                mysqli_free_result($result);
 /** query performs the db query
  * @param string sql
  * @param array data
  * @return this class
  */
 public function query($sql, $data = array())
 {
     try {
         if (!is_array($data)) {
             $data = array($data);
         }
         if (count($data)) {
             $sql = vsprintf($sql, $data);
         }
         $this->info($sql);
         $this->query = $sql;
         if (!$this->query) {
             throw new Exception(_('No query sent'));
         }
         $this->queryResult = $this->link->query($this->query, DATABASE_CONNTYPE);
         if (!$this->queryResult) {
             throw new Exception(_('Error: ') . $this->sqlerror());
         }
         if (DATABASE_CONNTYPE === MYSQLI_ASYNC) {
             $all_links[] = $this->link;
             $processed = 0;
             do {
                 $links = $errors = $reject = array();
                 foreach ($all_links as $link) {
                     $links[] = $errors[] = $reject[] = $link;
                 }
                 if (0 == ($ready = mysqli_poll($links, $errors, $reject, 1, 0))) {
                     usleep(1);
                     continue;
                 }
                 foreach ($links as $k => $link) {
                     $this->queryResult = $link->reap_async_query();
                     $processed++;
                 }
             } while ($processed < 1);
         }
     } catch (Exception $e) {
         $this->debug(sprintf('Failed to %s: %s', __FUNCTION__, $e->getMessage()));
     }
     return $this;
 }
        break;
    }
} while ($processed < 2);
// Killing connection - 3
$link = get_connection();
$thread_id = mysqli_thread_id($link);
mysqli_kill(get_connection(), $thread_id);
// Sleep 0.1s  to ensure the KILL gets recognized
usleep(100000);
if (false !== ($tmp = mysqli_query($link, "SELECT 1 AS 'processed before killed'", MYSQLI_ASYNC | MYSQLI_USE_RESULT))) {
    printf("[017] Expecting boolean/false got %s/%s\n", gettype($tmp), var_export($tmp, true));
}
$links = array($link);
$errors = array($link);
$reject = array($link);
if (0 !== ($tmp = mysqli_poll($links, $errors, $reject, 0, 10000))) {
    printf("[018] Expecting int/0 got %s/%s\n", gettype($tmp), var_export($tmp, true));
}
if (!is_array($links) || empty($links)) {
    printf("[019] Expecting non-empty array got %s/%s\n", gettype($links), var_export($links, true));
} else {
    foreach ($links as $link) {
        if (is_object($res = mysqli_reap_async_query($link))) {
            // No, you cannot fetch the result
            var_dump(mysqli_fetch_assoc($res));
            mysqli_free_result($res);
        } else {
            if ($link->errno > 0) {
                // But you are supposed to handle the error the way its shown here!
                printf("[020] Error: %d/%s\n", $link->errno, $link->error);
            }
foreach ($result as $key => $value) {
    $obj = new mysqli($host, $user, $password, $database);
    $links[spl_object_hash($obj)] = array('value' => $key, 'link' => $obj);
}
$done = 0;
$total = count($links);
foreach ($links as $value) {
    $value['link']->query("SELECT COUNT(*) AS `total` FROM `demo` WHERE `value`={$value['value']}", MYSQLI_ASYNC);
}
do {
    $tmp = array();
    foreach ($links as $value) {
        $tmp[] = $value['link'];
    }
    $read = $errors = $reject = $tmp;
    $re = mysqli_poll($read, $errors, $reject, 1);
    if (false === $re) {
        die('mysqli_poll failed');
    } elseif ($re < 1) {
        continue;
    }
    foreach ($read as $link) {
        $sql_result = $link->reap_async_query();
        if (is_object($sql_result)) {
            $sql_result_array = $sql_result->fetch_array(MYSQLI_ASSOC);
            //只有一行
            $sql_result->free();
            $hash = spl_object_hash($link);
            $key_in_result = $links[$hash]['value'];
            $result[$key_in_result] = $sql_result_array['total'];
        } else {
var_dump(mysqli_query($mysqli2, "SELECT SLEEP(0.20)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
$processed = $loops = 0;
$all = array($mysqli1, $mysqli2);
do {
    $loops++;
    if ($loops > 10) {
        printf("[006] The queries should have finished already\n");
        break;
    }
    $links = $errors = $reject = $all;
    ob_start();
    if (0 == ($ready = mysqli_poll($links, $errors, $reject, 0, 50000))) {
        $tmp = ob_get_contents();
        ob_end_clean();
        if ($tmp != '') {
            printf("Expected error:\n%s\n", $tmp);
            break;
        }
        continue;
    }
    foreach ($links as $link) {
        if ($res = mysqli_reap_async_query($link)) {
            mysqli_free_result($res);
        }
        $processed++;
    }
} while ($processed < 2);
$ready = mysqli_poll($links, $errors, $reject, 0, 50000);
mysqli_close($mysqli1);
mysqli_close($mysqli2);
print "done!";
 /**
  * @param TimerInterface $timer
  * @param int $status
  * @return $this
  */
 public function __invoke()
 {
     $links = $errors = $reject = [];
     foreach ($this->getAllConnections() as $connection) {
         /** @var ConnectionInterface $connection */
         if (($resource = $this->getAllConnections()[$connection]->poll()) === null) {
             continue;
         }
         $links[] = $errors[] = $reject[] = $resource;
     }
     if (!count($links)) {
         return $this;
     }
     if (\mysqli_poll($links, $errors, $reject, 0, $this->getTimeout())) {
         foreach ($links as $resource) {
             if (($result = $resource->reap_async_query()) === null) {
                 continue;
             }
             if (isset($this->connectionList[$resource])) {
                 $this->connectionList[$resource]->run($result);
             }
             if (!is_object($result)) {
                 mysqli_free_result($result);
             }
         }
     }
     return $this;
 }