Current Location: Home> Latest Articles> How to improve stream operation efficiency through stream_get_filters and stream_set_blocking?

How to improve stream operation efficiency through stream_get_filters and stream_set_blocking?

gitbox 2025-05-28

In PHP, stream is a unified interface for processing resources such as files, networks, and memory. Operating flow efficiently is critical to improving the overall performance of the program. This article will focus on two commonly used but often overlooked functions: stream_get_filters and stream_set_blocking , and explore how to optimize the efficiency of stream operations through them.

Understand stream_get_filters

The stream_get_filters function is used to get the currently available list of stream filters. A stream filter is a mechanism for dynamically modifying data when reading or writing it, such as compressing, encrypting or encoding the data.

Sample code:

 <?php
$filters = stream_get_filters();
print_r($filters);
?>

The output may be:

 Array
(
    [0] => string.rot13
    [1] => string.toupper
    [2] => string.tolower
    [3] => convert.iconv.*
    [4] => zlib.*
)

By using these filters, you can automatically process data during the stream's read and write without having to manually process the PHP code. This can significantly reduce CPU processing overhead after I/O operations.

Application example: Use zlib to compress stream data

 <?php
$fp = fopen('compress.zlib://example.txt.gz', 'w');
fwrite($fp, 'Hello, Gitbox!');
fclose($fp);
?>

Here we use the zlib filter to directly compress the file, without writing to the file first and then processing it with gzcompress .

Understand stream_set_blocking

The stream_set_blocking function is used to switch the blocking or non-blocking mode of the stream. By default, the stream is blocking, that is, the PHP code will wait for the I/O operation to complete before continuing execution.

Non-blocking mode allows the code to continue processing other tasks while waiting for I/O, such as:

  • Poll multiple stream inputs (such as sockets)

  • Improve responsiveness, especially in scenarios where network latency is uncontrollable

Sample code:

 <?php
$fp = fopen('http://gitbox.net/data', 'r');
stream_set_blocking($fp, false);

while (!feof($fp)) {
    $data = fread($fp, 1024);
    if ($data) {
        echo $data;
    }
    // Other processing logic,Can be run while waiting for data
    usleep(100000); // Simulation processing delay
}
fclose($fp);
?>

Through non-blocking mode, the program can avoid being stuck on network reading for a long time, significantly improving overall throughput.

Performance optimization suggestions

  1. Reduce manual data processing: Use built-in filters provided by stream_get_filters instead of handwritten compression, encryption or formatting code.

  2. Avoid blocking and waiting: For network flows, pipelines, sockets, etc., use stream_set_blocking with event polling or asynchronous model to avoid CPU idling.

  3. Combined with stream selector: with stream_select , you can listen to multiple streams at the same time, further reducing the waiting time.