stream_select
Description
The stream_select of Stream for PHP runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec.
Syntax
stream_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] ) : int
Parameters
read
The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).
write
The streams listed in the write array will be watched to see if a write will not block.
except
The streams listed in the except array will be watched for high priority exceptional ("out-of-band") data arriving.
Note: When stream_select() returns, the arrays read, write and except are modified to indicate which stream resource(s) actually changed status.
tv_sec
The tv_sec and tv_usec together form the timeout parameter, tv_sec specifies the number of seconds while tv_usec the number of microseconds. The timeout is an upper bound on the amount of time that stream_select() will wait before it returns. If tv_sec and tv_usec are both set to 0, stream_select() will not wait for data - instead it will return immediately, indicating the current status of the streams.
If tv_sec is NULL stream_select() can block indefinitely, returning only when an event on one of the watched streams occurs (or if a signal interrupts the system call).
Warning: Using a timeout value of 0 allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a 0 timeout value in a loop as it will cause your script to consume too much CPU time. It is much better to specify a timeout value of a few seconds, although if you need to be checking and running other code concurrently, using a timeout value of at least 200000 microseconds will help reduce the CPU usage of your script. Remember that the timeout value is the maximum time that will elapse; stream_select() will return as soon as the requested streams are ready for use.
tv_usec
See tv_sec description.
Return
On success stream_select() returns the number of stream resources contained in the modified arrays, which may be zero if the timeout expires before anything interesting happens. On error FALSE is returned and a warning raised (this can happen if the system call is interrupted by an incoming signal).
Examples
1 · read write except tv_sec
<? $filename = "https://osbo.com"; $mode = "r"; $handle1 = fopen($filename, $mode); $handle2 = fopen($filename, $mode); $read = array($handle1, $handle2); $write = null; $except = null; $tv_sec = 0; $return = stream_select($read, $write, $except, $tv_sec); var_export($return); fclose($handle2); fclose($handle1); ?>
2 · tv_usec
<? $filename = "https://osbo.com"; $mode = "r"; $handle1 = fopen($filename, $mode); $handle2 = fopen($filename, $mode); $read = array($handle1, $handle2); $write = null; $except = null; $tv_sec = 0; $tv_usec = 200000; $return = stream_select($read, $write, $except, $tv_sec, $tv_usec); var_export($return); fclose($handle2); fclose($handle1); ?>
Links
Stream
- stream_bucket_append
- stream_bucket_make_writeable
- stream_bucket_new
- stream_bucket_prepend
- stream_context_create
- stream_context_get_default
- stream_context_get_options
- stream_context_get_params
- stream_context_set_default
- stream_context_set_option
- stream_context_set_options
- stream_context_set_params
- stream_copy_to_stream
- stream_filter_append
- stream_filter_prepend
- stream_filter_register
- stream_filter_remove
- stream_get_contents
- stream_get_filters
- stream_get_line
- stream_get_meta_data
- stream_get_transports
- stream_get_wrappers
- stream_is_local
- stream_isatty
- stream_notification_callback
- stream_register_wrapper
- stream_resolve_include_path
- stream_set_blocking
- stream_set_chunk_size
- stream_set_read_buffer
- stream_set_timeout
- stream_set_write_buffer
- stream_socket_accept
- stream_socket_client
- stream_socket_enable_crypto
- stream_socket_get_name
- stream_socket_pair
- stream_socket_recvfrom
- stream_socket_sendto
- stream_socket_server
- stream_socket_shutdown
- stream_supports_lock
- stream_wrapper_register
- stream_wrapper_restore
- stream_wrapper_unregister