Brief Introduction to PHP non-blocking mode _ php instance

Source: Internet
Author: User
The non-blocking mode refers to the use of the socket event message mechanism. The communication between the Server and the Client is asynchronous. This article introduces the non-blocking mode of php, if you are interested in learning it together, the non-blocking mode means that the communication between the Server and the Client is asynchronous by using the message mechanism of the socket event.

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.