PHP Streams 中的阻塞和非阻塞模式
我正在准备 PHP 5 认证考试。这个功能在练习考试中提到过。
函数stream_set_blocking():
设置阻塞或非阻塞模式 一个流。
该函数适用于任何流 支持非阻塞模式 (目前,常规文件和套接字 流)。
从高层和底层的角度来看,阻塞模式和非阻塞模式流在 PHP 中的行为如何?什么是套接字流和非套接字流?示例值得赞赏。
I am studying for the PHP 5 Certification exam. This function was mentioned in the practice exams.
function stream_set_blocking():
Sets blocking or non-blocking mode on
a stream.This function works for any stream
that supports non-blocking mode
(currently, regular files and socket
streams).
From both a high-level and low-level perspective, how do blocking mode and non-blocking mode streams behave in PHP? What i a socket stream and a non-socket stream? Examples are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
阻塞/非阻塞模式表示 fread/fwrite 函数是否立即返回。当处于
非阻塞模式
时,它们将返回任何可用数据。如果调用函数时无法读取数据,则不会返回任何数据。此类流通常在循环中轮询。然而,在
阻塞模式
中,该函数将始终等待(因此阻塞程序执行),直到它可以满足完整的读取请求。如果您要求从网络套接字读取 1MB,则该函数只有在收到要传递的 1MB 数据后才会返回。我认为维基百科很好地涵盖了它:
http://en.wikipedia.org/wiki/Berkeley_sockets#Blocking_vs._non-blocking_mode 主要对网络文件/流源产生影响。对于本地文件系统,操作系统将始终读取所需长度的数据。 PHP 还具有 流包装器,它可以自行处理该选项(有没有可靠的一般规则)。
有关更多底层详细信息,请访问 fnctl(2) 或 socket(2) 或
的联机帮助页
http://www.scottklement.com/rpg/socktut/nonblocking.html
The blocking/non-blocking mode says if the fread/fwrite functions will return immediately. When in
non-blocking mode
, they will return any available data. If no data can be read at the time the function is invoked, then none will be returned. Such streams are typicalled polled in a loop.In
blocking mode
however, the function will always wait (and therefore block your programs execution) until it can satisfy the complete read request. If you ask to read 1MB from a network socket, the function will not return until it has received 1MB to pass on.I think Wikipedia covers it quite well:
http://en.wikipedia.org/wiki/Berkeley_sockets#Blocking_vs._non-blocking_mode
It mostly has effect on networked file/stream sources. For local filesystems the operating system will always read the desired length of data. PHP also has stream wrappers, which can handle that option at their discretion (there's no reliable general rule).
For more low-level details, visit the manpages of fnctl(2) or socket(2) or
http://www.scottklement.com/rpg/socktut/nonblocking.html