A brief introduction to PHP non-blocking mode and a brief introduction to php blocking. A brief introduction to the non-blocking mode of PHP. a brief introduction to the non-blocking mode of php refers to the asynchronous communication between the Server and Client using the message mechanism of socket events. Let PHP not briefly introduce the non-blocking mode of PHP, but briefly introduce php blocking
The non-blocking mode refers to the use of the socket event message mechanism. communication between the Server and the Client is asynchronous.
PHP is no longer blocked. When PHP is processed as the backend, some long-term processing is required. in order to quickly respond to page requests without making a result return judgment, the following measures can be taken:
1. if you are using the FastCGI mode, use fastcgi_finish_request () to end the session immediately, but the PHP thread continues to run.
echo "program start.";file_put_contents('log.txt','start-time:'.date('Y-m-d H:i:s'), FILE_APPEND);fastcgi_finish_request();sleep(1);echo 'debug...';file_put_contents('log.txt', 'start-proceed:'.date('Y-m-d H:i:s'), FILE_APPEND);sleep(10);file_put_contents('log.txt', 'end-time:'.date('Y-m-d H:i:s'), FILE_APPEND);
In this example, the output result shows that the output program start.will be returned. the debug's output viewer cannot receive the result, and the log.txt file can receive three complete completion times.
2. use the non-blocking mode of fsockopen and cUrl to request another URL
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);if (!$fp) die('error fsockopen');stream_set_blocking($fp,0);$http = "GET /save.php / HTTP/1.1\r\n"; $http .= "Host: www.example.com\r\n"; $http .= "Connection: Close\r\n\r\n";fwrite($fp,$http);fclose($fp);
Use the curl_multi _ * function in cURL to send asynchronous requests
$cmh = curl_multi_init();$ch1 = curl_init();curl_setopt($ch1, CURLOPT_URL, "http://localhost:6666/child.php");curl_multi_add_handle($cmh, $ch1);curl_multi_exec($cmh, $active);echo "End\n";
3. use Gearman and Swoole extensions
Gearman is a distributed asynchronous processing framework with php extensions that can process large batches of asynchronous tasks;
Swoole is very popular recently and has many asynchronous methods, which are easy to use. (Dust margin note: Said to redefine PHP, NodeJS spray completely. Although the Swoole tool is good, it feels that the extension itself is not comparable with NodeJS)
4. use redis and other caches and queues to write data into the cache and implement asynchronous data processing using scheduled tasks in the background.
This method should be common in common large-traffic architectures.
5. in extreme cases, you can call system commands to send data to backend tasks for execution. in my personal opinion, it is not very efficient.
$cmd = 'nohup php ./processd.php $someVar >/dev/null &';`$cmd`
6. foreign experts did not understand the big tricks. php native support
Http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html
VII. install the pcntl extension and use pcntl_fork to generate sub-processes for asynchronous task execution. I personally think it is the most convenient, but zombie process is also prone.
If ($ pid = pcntl_fork () = 0) {child_func (); // subprocess function, main process running} else {father_func (); // Main Process function} echo "Process ". getmypid (). "get to the end. \ n "; function father_func () {echo" Father pid is ". getmypid (). "\ n";} function child_func () {sleep (6); echo "Child process exit pid is ". getmypid (). "\ n"; exit (0 );}
The above content is the PHP non-blocking mode introduced by the small Editor. I hope it will help you!
The callback non-blocking mode refers to the use of the socket event message mechanism. communication between the Server and the Client is asynchronous. Stop PHP...