[转帖]mysqli::poll_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1328 | 回复: 0   主题: [转帖]mysqli::poll        下一篇 
ad222888
注册用户
等级:新兵
经验:66
发帖:134
精华:0
注册:2016-9-25
状态:离线
发送短消息息给ad222888 加好友    发送短消息息给ad222888 发消息
发表于: IP:您无权察看 2018-8-6 10:20:40 | [全部帖] [楼主帖] 楼主

mysqli::poll
mysqli_poll
(PHP 5 >= 5.3.0, PHP 7)
mysqli::poll -- mysqli_poll — Poll connections


面向对象风格

public
static
int
mysqli::poll (
array &$read ,
array &$error ,
array &$reject ,
int $sec [,
int $usec ] )


过程化风格

int
mysqli_poll (
array &$read ,
array &$error ,
array &$reject ,
int $sec [,
int $usec ] )


Poll connections. 仅可用于 mysqlnd。 The method can be used as static.

read
List of connections to check for outstanding results that can be read.
error
List of connections on which an error occured, for example, query failure or lost connection.
reject
List of connections rejected because no asynchronous query has been run on for which the function could poll results.
Maximum number of seconds to wait, must be non-negative.
usec
Maximum number of microseconds to wait, must be non-negative.


返回值

 Returns number of ready connections upon success, FALSE otherwise.
Example #1 A mysqli_poll() example
<?php
$link1 = mysqli_connect();
$link1->query("SELECT 'test'", MYSQLI_ASYNC);
$all_links = array($link1);
$processed = 0;
do {
      $links = $errors = $reject = array();
      foreach ($all_links as $link) {
            $links[] = $errors[] = $reject[] = $link;
      }
      if (!mysqli_poll($links, $errors, $reject, 1)) {
            continue;
      }
      foreach ($links as $link) {
            if ($result = $link->reap_async_query()) {
                  print_r($result->fetch_row());
                  if (is_object($result))
                  mysqli_free_result($result);
            } else die(sprintf("MySQLi Error: %s", mysqli_error($link)));
            $processed++;
      }
} while ($processed < count($all_links));
?>


以上例程会输出:

mysqli_query() - 对数据库执行一次查询

 mysqli_reap_async_query() - Get result from async query
add a note
User Contributed Notes 4 notes
Sometime it's not clear what does it mean as connection is "ready" . Does it mean the query completed or just some records ready to read?
Is it possible to use asynchronous (unblocking) and unbuffered SELECT-query? I.e. with both MYSQLI_ASYNC|MYSQLI_USE_RESULT
I want some poll-like code inside my loop that has four case-options:
1. Poll wait time-out is over but no records of result set is ready.
2. One or more records are ready to read (but the query still running).
3. Query is successfully over (completed; no more records).
4. Error.
Is it possible?
As I understood the operation of reading a record from unbuffered query is blocking if there are no ready records at the moment, and there is no functions to get out how much records are ready ?
You can use the following code to execute, for example, 10 simultaneous queries:
$query = "SELECT `field1`, `field2` FROM `table` WHERE `field1`='something'";
$all_links = array();
for($i=0; $i<10; $i++) {
      $link = mysqli_connect("your.mysql.server.here","your@user","pa$$w0rd",DataBase_Name");
      $link->query($query, MYSQLI_ASYNC);
      $all_links[] = $link;
}
$processed = 0;
do {
      $links = $errors = $reject = array();
      foreach ($all_links as $link) {
            $links[] = $errors[] = $reject[] = $link;
      }
      if (!mysqli_poll($links, $errors, $reject, 1)) {
            continue;
      }
      foreach ($links as $link) {
            if ($result = $link->reap_async_query()) {
                  print_r($result->fetch_row());
                  if (is_object($result))
                  mysqli_free_result($result);
            } else die(sprintf("MySQLi Error: %s", mysqli_error($link)));
            $processed++;
      }
} while ($processed < count($all_links));
NOTE: If you get a "Warning: mysqli::query() expects parameter 2 to be long, string given" you have a mysqlnd installation or configuration issue.
Improved version of using polling to check for slow query,
"KILL #ID" does not work if id of the myqsli link is the same as the one that kills it,
so you have to disconnect from mysqli and connect again with new link to
kill the #ID.
<?php
// Slow SQL Query
$SelectSql = 'SELECT * FROM SLOW_QUERY';
$link = mysqli_connect('localhost','user','pass','database');
mysqli_query($SelectSql, MYSQLI_ASYNC);
$thread_id = mysqli_thread_id($link);
// Ignore user abort so we can kill the query
ignore_user_abort(true);
$MaxTime = 5; // seconds
$Overtime = false;
$StartTime = time();
do
{
      // Poll MySQL
      $links = $errors = $reject = array($link);
      $poll = mysqli_poll($links, $errors, $reject, 0, 500000);
      // Check if the connection is aborted and the query was killed
      if (connection_aborted()) {
            $link_new = mysqli_connect('localhost','user','pass','database');
            mysqli_kill($link_new, $thread_id);
            $kill = mysqli_kill($link_new, $thread_id);
            if ($kill)
            {
                  die();
            }
      }
      $EndTime = time();
      // Check overtime, kill if detected overtime
      if ($EndTime - $StartTime > $MaxTime)
      {
            $link_new = mysqli_connect('localhost','user','pass','database');
            mysqli_kill($link_new, $thread_id);
            $Overtime = true;
            echo 'Error: Query took over '.$Overtime.'.';
      }
} while (!$poll && $Overtime == false);
?>
This is how you can test and terminate slow query with polling.
<?php
// Slow SQL Query
$SelectSql = 'SELECT * FROM SLOW_QUERY';
$link = mysqli_connect('localhost','user','pass','database');
mysqli_query($SelectSql, MYSQLI_ASYNC);
$thread_id = mysqli_thread_id($link);
// Ignore user abort so we can kill the query
ignore_user_abort(true);
$MaxTime = 5; // seconds
$Overtime = false;
$StartTime = time();
do
{
      // Poll MySQL
      $links = $errors = $reject = array($link);
      $poll = mysqli_poll($links, $errors, $reject, 0, 500000);
      // Check if the connection is aborted and the query was killed
      if (connection_aborted() && mysqli_kill($link, $thread_id)) {
            die();
      }
      $EndTime = time();
      // Check overtime, kill if detected overtime
      if ($EndTime - $StartTime > $MaxTime)
      {
            mysqli_kill($link, $thread_id);
            $Overtime = true;
            echo 'Error: Query took over '.$Overtime.'.';
      }
} while (!$poll && $Overtime == false);
?>




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论