note 117492 added to function.stream-select
| From: | doingitwrong at mailismagic dot com | Date: | Wed, 17 Jun 2015 22:35:35 +0000 |
| Subject: | note 117492 added to function.stream-select | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202892@lists.php.net to get a copy of this message | ||
If you use stream_select() with a blocking stream, you are doing it wrong!
Just because this function returns something in one or more of the arrays does NOT mean that a
future read or write operation will not block.
The above sentence is the most important sentence you will ever read regarding stream manipulation.
Using stream_select() with blocking streams is a very common amateur mistake and causes major
headaches when tracking down usage of this and similar select() system functions. PHP (and really
the underlying OS) should verify that the supplied stream set is not blocking and throw an
error/exception if any socket is set to block so that people are forced to fix their code. The
documentation for stream_select() is, at best, misleading.
If you want a non-blocking stream, then set the stream to not block. Otherwise, live with the
blocking stream. That is, after all, the whole point of blocking - to block indefinitely until the
operation completes. select() is built for non-blocking streams ONLY. Any other use will result in
very hard to track down bugs.
I got the above lecture many years ago after encountering the very bugs I mention. I fixed my code
and now correct similar mistakes when I run into the issue elsewhere. Writing code for non-blocking
streams is simpler than trying to write hacks for blocking streams with select() functions and
ending up with application bugs.
----
Server IP: 72.52.91.14
Probable Submitter: 207.225.254.78
----
Manual Page -- http://php.net/manual/en/function.stream-select.php
Edit -- https://master.php.net/note/edit/117492
Del: integrated -- https://master.php.net/note/delete/117492/integrated
Del: useless -- https://master.php.net/note/delete/117492/useless
Del: bad code -- https://master.php.net/note/delete/117492/bad+code
Del: spam -- https://master.php.net/note/delete/117492/spam
Del: non-english -- https://master.php.net/note/delete/117492/non-english
Del: in docs -- https://master.php.net/note/delete/117492/in+docs
Del: other reasons-- https://master.php.net/note/delete/117492
Reject -- https://master.php.net/note/reject/117492
Search -- https://master.php.net/manage/user-notes.php