PHP 中的 stream_select() 问题 [英] issue with stream_select() in PHP

查看:31
本文介绍了PHP 中的 stream_select() 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 stream_select() 但它在几秒钟后返回 0 个描述符,而我的函数仍然有数据要读取.

I am using stream_select() but it returns 0 number of descriptors after few seconds and my function while there is still data to be read.

一个不寻常的事情是,如果你将超时设置为 0,那么我总是得到描述符的数量为零.

An unusual thing though is that if you set the time out as 0 then I always get the number of descriptors as zero.

$num = stream_select($read, $w, $e, 0);

推荐答案

stream_select() 必须在循环中使用

stream_select()函数基本上只是轮询您在前三个参数中提供的流选择器,这意味着它将等到以下事件之一发生:

stream_select() must be used in a loop

The stream_select() function basically just polls the stream selectors you provided in the first three arguments, which means it will wait until one of the following events occur:

  • 一些数据到达
  • 或者在没有获取任何数据的情况下达到超时(使用 $tv_sec 和 $tv_usec 设置).

所以收到0作为返回值完全正常,表示当前轮询周期没有新数据.

So recieving 0 as a return value is perfectly normal, it means there was no new data in the current polling cycle.

我建议将函数放入这样的循环中:

I'd suggest to put the function in a loop something like this:

$EOF = false;

do {
    $tmp  = null;
    $ready = stream_select($read, $write, $excl, 0, 50000);

    if ($ready === false ) {
        // something went wrong!!
        break;
    } elseif ($ready > 0) {
        foreach($read as $r) {
            $tmp .= stream_get_contents($r);
            if (feof($r)) $EOF = true;
        }

        if (!empty($tmp)) {
            //
            // DO SOMETHING WITH DATA
            //
            continue;
        }
    } else {
        // No data in the current cycle
    }
} while(!$EOF);

请注意,在此示例中,脚本完全忽略了输入流之外的所有内容.此外,if"语句的第三部分完全是可选的.

Please note that in this example, the script totally ignores everything aside from the input stream. Also, the third section of the "if" statement is completely optional.

这篇关于PHP 中的 stream_select() 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆