Home > Backend Development > PHP Tutorial > 说好的fsockopen的非阻塞模式呢?为什么还是会阻塞

说好的fsockopen的非阻塞模式呢?为什么还是会阻塞

WBOY
Release: 2016-06-23 14:00:21
Original
1003 people have browsed it

function fsockopen_test(){    $hostname = 'm.cn';    $url = '/test/1.php';    $fp = fsockopen($hostname, 80, $errno, $errstr, 5);    if (!$fp) {        echo "$errstr ($errno)";        return false;    }    stream_set_blocking($fp,0);//开启了手册上说的非阻塞模式    $header = "GET $url HTTP/1.1\r\n";     $header.="Host: $hostname\r\n";    $header.="Connection: Close\r\n\r\n";//长连接关闭    fwrite($fp, $header);    fclose($fp);}fsockopen_test();
Copy after login

请求的/test/1.php里面的代码:
sleep(6);$fp  =  fopen ( './data.txt' ,  'w' );fwrite ( $fp ,111);fclose ( $fp );
Copy after login
通过浏览器控制台查看实际还是会等test/1.php执行完毕,这边才刷新。这是为什么呢?


回复讨论(解决方案)

没问题呀

echo date('Y-m-d H:i:s'), PHP_EOL;fsockopen_test();echo date('Y-m-d H:i:s'), PHP_EOL;
Copy after login
Copy after login
Copy after login
Copy after login
可以看到没有延时

没问题呀

echo date('Y-m-d H:i:s'), PHP_EOL;fsockopen_test();echo date('Y-m-d H:i:s'), PHP_EOL;
Copy after login
Copy after login
Copy after login
Copy after login
可以看到没有延时 额?我这边怎么有呢?

没问题呀

echo date('Y-m-d H:i:s'), PHP_EOL;fsockopen_test();echo date('Y-m-d H:i:s'), PHP_EOL;
Copy after login
Copy after login
Copy after login
Copy after login
可以看到没有延时 我修改成你的代码,然后把原来的输出注释了,发现还是有延时呢

没问题呀

echo date('Y-m-d H:i:s'), PHP_EOL;fsockopen_test();echo date('Y-m-d H:i:s'), PHP_EOL;
Copy after login
Copy after login
Copy after login
Copy after login
可以看到没有延时
找到原因了,把这段代码注释掉就OK了
function fsockopen_test(){    $hostname = 'm.cn';    $url = '/test/1.php';    $fp = fsockopen($hostname, 80, $errno, $errstr, 5);    if (!$fp) {        echo "$errstr ($errno)";        return false;    }    stream_set_blocking($fp,0);//开启了手册上说的非阻塞模式    $header = "GET $url HTTP/1.1\r\n";     $header.="Host: $hostname\r\n";    $header.="Connection: Close\r\n\r\n";//长连接关闭    fwrite($fp, $header);    /*$html = '';    while (!feof($fp)) {        $html.=fgets($fp);    }    echo $html;*/    fclose($fp);}echo time(),'<br>';fsockopen_test();echo time(),'<br>';
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template