mysqli_poll() - 第三个参数是什么?

时间:2013-07-26 23:38:52

标签: php asynchronous mysqli

在没有任何文档beyond a function prototype的情况下,我很难找到mysqli_poll()函数的第三个参数是什么。

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

查看(C)源代码,它似乎用$ ......填充$ reject数组......

CONN_GET_STATE((*p)->data) <= CONN_READY 
   || CONN_GET_STATE((*p)->data) == CONN_QUIT_SENT

这是否意味着与服务器的连接正在关闭/关闭?

还有别的吗?

是否应预先设置资源以检查是否已断开连接?或者它们会自动从$ read和$ error中添加?

3 个答案:

答案 0 :(得分:3)

数组绑定到select()系统调用。

如您所见:http://lxr.php.net/xref/PHP_5_5/ext/mysqlnd/mysqlnd.c#1384

所以,是的,根据select(2)人员文件:

  

Select()检查其地址在readfds,writefds和errorfds中传递的I / O描述符集,以查看它们的某些描述符是否已准备好进行读取,是否已准备好写入,或者是否具有待处理的异常条件。

writefds在这里并不重要,在实施中会被忽略)

如果另一个数组填充了mysqli对象,则允许$read$error数组为空。

$read必须是一个包含你要民意调查的mysqli对象的数组。

$error必须是一个包含你想要检查非常数据的mysqli对象的数组。

正如您从http://lxr.php.net/xref/PHP_5_5/ext/mysqlnd/mysqlnd_enum_n_def.h#322http://lxr.php.net/xref/PHP_5_5/ext/mysqlnd/mysqlnd.c#1228看到的那样,与<= CONN_READY || == CONN_QUIT_SENT不匹配的所有内容都意味着CONN_QUERY_SENT, CONN_SENDING_LOAD_DATA, CONN_FETCHING_DATA, CONN_NEXT_RESULT_PENDING(即数据尚未准备好提取;名称应该是自解释)不会被添加到就绪数组==将是$rejected

将它传递给函数时,

$rejected可以是任何东西;它将被一个数组覆盖,其中$read中的条目尚未准备好被读取。

答案 1 :(得分:1)

我相信被拒绝的主题最终会被放弃。

  

查看此示例:   https://svn.osgeo.org/mapguide/sandbox/rfc94/Oem/php/ext/mysqli/tests/mysqli_poll.phpt

答案 2 :(得分:0)

我建立了一个试验台:

$l1 = mysqli_connect();
$l2 = mysqli_connect();
$l3 = mysqli_connect();

$s1 = "SELECT CURTIME()";
$s2 = "SELECT * FROM";   // will error #1064
$s3 = "SELECT SLEEP(10), CURTIME()";

mysqli_query($l1, $s1, MYSQLI_ASYNC);
mysqli_query($l2, $s2, MYSQLI_ASYNC);
mysqli_query($l3, $s3, MYSQLI_ASYNC);

$started=time();

for ($x=0; $x<5; $x++) {
        $ready=$reject=$errors = array($l1, $l2, $l3);
        print "\niteration $x at t+" . (time()-$started) . "\n";
        mysqli_poll($ready, $errors, $reject, 3);
        print "ready = " . count($ready) . "\n";
        foreach($ready as $r) {
           $c=mysqli_reap_async_query($r);
           print "err=" . mysqli_error($r) . " cnt=" . count($c) . "\n";
        };
        print "errors = " . count($errors) . "\n";
        print "reject = " . count($reject) . "\n";
        sleep(4);
 }

结果(以//为前缀的注释):

iteration 0 at t+0
ready = 1              // appears to be SELECT CURTIME()
err= cnt=1
errors = 0
reject = 0

iteration 1 at t+4
ready = 1              // appears to be SELECT * FROM
err=You have an error in your SQL syntax; check the manual that corresponds 
  to your MySQL server version for the right syntax to use near '' at line 
  1 cnt=1
errors = 0
reject = 1             // appears to be SELECT CURTIME() (results reaped)

iteration 2 at t+8
ready = 1              // appears to be SELECT SLEEP(10) despite 8 seconds elapsed
err= cnt=1
errors = 0
reject = 2             // appears to be SELECT CURTIME() + SELECT * FROM

iteration 3 at t+14
ready = 0
errors = 0
reject = 3

iteration 4 at t+21
ready = 0
errors = 0
reject = 3

即。 $ reject正在填充已获得结果的链接,而不是尚未准备好轮询的链接。

尝试使用无效连接,将从所有数组中删除该值(不添加到$ reject),