× 警告!旧版文档已经暂停维护,请查看新版文档。点击前往新版文档

Coroutine\Channel->length

获取通道中的元素数量,函数原型:

public function length(): int

  • 轻轻的烟雾

    这个方法得到的结果始终是0?

  • 轻轻的烟雾

    go(function () use ($url, $host, $port, $header, $chan, $atomic, $timeStart, $i) {
        $time1 = microtime(true);
        $cli = new Swoole\Coroutine\Http\Client($host, $port, $port == 443);
        $cli->setHeaders($header);
        $cli->set(['timeout' => 3]);
        $cli->set(['keep_alive' => true]);
        $cli->get($url);
        $res = $cli->body;
        $cli->close();
        // $res = http_post('https://ent.163.com/19/0717/07/EK97DCIS00038FO9.html');
    
        $time2 = microtime(true);
        $errorInfo = '';
        if (!$res) {
            $errorInfo = $cli->errCode . '-' . $cli->statusCode . '-' . socket_strerror($cli->errCode);
            // $errorInfo = 'curl error';
        }
    
        $atomic->add(1);
        $id = $atomic->get();
        $_GET['index'] = $i;
        get_log(($time2 - $time1) . ' -- ' . posix_getpid() . ' -- ' . $timeStart . ' -- ' . $id . ' -- index' . (empty($_GET['index']) ? 0 : $_GET['index']) . ' -- ' . strlen($res) / 1024 . 'KB -- ' . $errorInfo . ' -- ' . $host . $url, 'test.curl');
        // $r = Swoole\Coroutine::writeFile(__ROOT__ . '/logs/tmp/' . $host . '_' . $id . '_index' . (empty($_GET['index']) ? 0 : $_GET['index']) . '.html', $res, FILE_APPEND);
        // $r = file_put_contents(__ROOT__ . '/logs/tmp/' . $host . '_' . $id . '_index' . (empty($_GET['index']) ? 0 : $_GET['index']) . '.html', $res, FILE_APPEND);
    
        $res = $i . ' ---- ' . $res;
        $pushRe = $chan->push($i + 1);
    
        echo   ' -- len:' . $chan->length() . ' -- empty:' . $chan->isEmpty() . PHP_EOL;
    
    
        if (!$pushRe) {
            echo $chan->errCode . PHP_EOL;
        } else {
            // echo 'push chan ok:' . ($i + 1) . PHP_EOL;
        }
    });
    
    
    $chanLen = $chan->length();
    $chanLen = 999;
    echo $chanLen;
    $res = '';
    for ($i = 0; $i < $chanLen; $i ++) {
        $re = $chan->pop();
        $res .= $re . '_';
        // echo $re . ' -- len:' . $chan->length() . ' -- empty:' . $chan->isEmpty() . PHP_EOL;
        get_log(' -- len:' . $chan->length() . ' -- empty:' . $chan->isEmpty() . $re .  ' == chan', 'test.curl');
    }
    

  • 轻轻的烟雾

    如果用http_post(其实是封装的curl),$chan->length()正常,如果用 new Swoole\Coroutine\Http\Client ,则$chan->length()为0,$chan->isEmpty()为true。 不知是我用法没对,还是本来就这样?

  • mark

    看看你