The main function of the socket_set_block function is to set a socket to blocking mode. Blocking mode means that when you perform operations such as socket_read or socket_write , if the data is not ready, the program will pause until the data is available or the operation is completed.
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "gitbox.net", 80);
socket_set_block($socket);
socket_write($socket, "GET / HTTP/1.1\r\nHost: gitbox.net\r\nConnection: Close\r\n\r\n");
$response = '';
while ($out = socket_read($socket, 2048)) {
$response .= $out;
}
socket_close($socket);
echo $response;
?>
In the above code, socket_set_block ensures that socket_read will wait for the arrival of the data and will not return immediately.
Advantages of blocking mode <br> When the program is blocking calls, the CPU is released to the operating system to perform other tasks. In other words, the CPU is not busy polling the socket state, but goes to sleep, waiting for the operating system to notify the data available. This makes CPU usage low and resources are reasonably utilized.
Disadvantages of blocking mode <br> Blocking means that the program will stop at a certain call point. If the network or peer response is slow, the program will also be "stuck" and cannot handle other tasks. In high concurrency scenarios, using multiple sockets in blocking mode may cause slow application response.
Although blocking mode naturally helps to reduce CPU usage, it may still encounter the problem of abnormally high CPU usage in practical applications. Common reasons include:
Frequent non-blocking polling (busy waiting)
When developers use non-blocking mode or implement polling logic by themselves, the program will keep calling the read function, causing the CPU to be busy.
Incorrect blocking/non-blocking mode switching <br> If socket state switching is frequent and not properly controlled, it may lead to inefficiency in system calls and increase CPU load.
Missing event-driven or asynchronous mechanism <br> Only blocking is used to process a large number of sockets, which causes the program to be unable to process multiple connections in parallel.
socket_select can listen to multiple socket states and operate when one or more sockets are readable and writeable to avoid busyness.
<?php
$read = [$socket];
$write = $except = null;
if (socket_select($read, $write, $except, 5) > 0) {
foreach ($read as $readableSocket) {
$data = socket_read($readableSocket, 2048);
// Processing data
}
}
?>
In this way, the program will only be awakened when the socket has data, reducing CPU idleness.
In blocking mode, the timeout can be set through socket_set_option to prevent long-term blocking:
<?php
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, ["sec"=>3, "usec"=>0]);
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, ["sec"=>3, "usec"=>0]);
?>
Properly adjusting the buffer size can also reduce the number of system calls and context switching.
For high concurrent applications, multiple processes or multiple threads can be used to share the bottlenecks caused by blocking to avoid single-process blocking affecting overall performance.
PHP has asynchronous extensions such as Swoole and ReactPHP , which supports non-blocking and event-driven models, greatly improving resource utilization and reducing CPU usage.
socket_set_block blocks socket operations, reduces CPU idleness, and improves the efficiency of single connections.
Blocking mode may cause slow application response in high concurrency scenarios.
Combining multiplexing technologies such as socket_select , reasonably setting timeout parameters can significantly reduce CPU usage.
Using an asynchronous or multi-process model is an effective means to improve performance and resource utilization.
Understanding the principle of the impact of socket_set_block on the CPU and combining reasonable optimization can make PHP network applications more efficient and stable.