Simple Introduction to PHP non-blocking mode _php Instance

Source: Internet
Author: User

Non-blocking mode refers to the use of the message mechanism of the socket event, the communication between the server and the client is in an asynchronous state.

Let PHP no longer block when PHP as a backend processing needs to complete some long time processing, in order to quickly respond to page requests, without the results returned to judge, you can have the following measures:

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 (a);
File_put_contents (' Log.txt ', ' end-time: '. Date (' y-m-d h:i:s '), file_append);

This example outputs the output program start. After the session is returned, debug that output browser is not received, and the Log.txt file can receive a full three completion time.

Second, the use of fsockopen, curl of non-blocking mode request another URL

$fp = Fsockopen ("www.example.com", $errno, $errstr,);
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);

To send an asynchronous request using the Curl_multi_* function in Curl

$CMH = Curl_multi_init ();
$ch 1 = curl_init ();
curl_setopt ($ch 1, Curlopt_url, "http://localhost:6666/child.php");
Curl_multi_add_handle ($CMH, $ch 1);
Curl_multi_exec ($CMH, $active);
echo "end\n";

Third, the use of Gearman, Swoole extension

Gearman is a distributed asynchronous processing framework with PHP extension, which can handle large batch asynchronous tasks.
Swoole is very hot recently, there are many asynchronous methods, easy to use. (Worldly note: Known as redefining PHP, the nodejs sprayed to pieces.) The Swoole tool is good, but it feels like the expansion itself is not comparable to Nodejs.

Using Redis cache, queue, write data to cache, use the background planning task to implement data asynchronous processing.

This method should be common in the common large traffic structure.

In extreme cases, you can call the system command, you can pass the data to the background task execution, personal feeling is not very efficient.

$cmd = ' nohup php./processd.php $someVar >/dev/null & ';
' $cmd '

Six, foreign guy's big recruit, did not understand, PHP native support

Http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html

Seven, install pcntl extension, use pcntl_fork build child process asynchronous execution task, personally feel is the most convenient, but also easy to appear zombie process.

if ($pid = Pcntl_fork ()) = = 0) {
child_func ();//child process function, main process run
} 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 small series to introduce the PHP non-blocking mode, I hope to 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.