A simple introduction to PHP non-blocking mode, a simple introduction to PHP blocking _php tutorial

Source: Internet
Author: User

Simple Introduction to PHP non-blocking mode, simple introduction of PHP blocking


nonblocking mode refers to the message mechanism that leverages the socket event, and the communication between the server side and client side is in an asynchronous state.

Let PHP no longer block when PHP as a back-end processing needs to complete some long time processing, in order to quickly respond to page requests, without making a result return judgment, you can have the following measures:

First, if you are using 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 (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, so the debug output browser is not received, and the Log.txt file can receive a complete three completion time.

Second, use Fsockopen, curl non-blocking mode to request additional URLs

$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);

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 extensions that can handle large batches of asynchronous tasks;
Swoole is very hot recently, there are many asynchronous methods, easy to use. (Chenyuan Note: It is known to redefine PHP, the Nodejs spray to pieces. Swoole tool is good, but feel is the extension itself and Nodejs not comparable sex)

Four, using the cache, such as Redis, queue, write data to the cache, using background scheduling task to implement data asynchronous processing.

This approach should be common in a common high-traffic architecture.

V. In extreme cases, the system commands can be invoked to pass data to the background task execution, and the personal feeling is not very efficient.

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

Six, the foreigner's big recruit, did not read, PHP native support

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

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

if ($pid = Pcntl_fork ()) = = 0) {child_func ();//Sub-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 to introduce you to the PHP non-blocking mode, we hope to help!

http://www.bkjia.com/PHPjc/1106132.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106132.html techarticle A simple introduction to PHP non-blocking mode, a simple introduction of PHP blocking non-blocking mode refers to the use of the socket event message mechanism, the server side and client side of the communication between the asynchronous state. Let PHP no longer ...

  • Related Article

    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.