8 ways to implement PHP non-blocking to quickly respond to page requests

Source: Internet
Author: User
Program non-blocking mode, which can also be understood as concurrency, in order to make PHP no longer blocked when PHP as a back-end processing needs to complete some long-time processing, in order to quickly respond to page requests, do not make results return judgment, can have the following measures:

Specific PHP non-blocking implementation method:

1 using Fastcgi_finish_request ()

If PHP and the WEB server use the PHP-FPM (FastCGI process Manager), the Fastcgi_finish_request () function can end the session immediately, and the PHP thread can continue to run in the background.

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

From the output results can be seen, the page finished printing program start ..., the output of the first line to Log.txt after the session is returned, so the debug behind ... is not displayed on the browser, and the Log.txt file can receive a complete three completion time.

2 using Fsockopen ()

Use Fsockopen () to open a network connection or a UNIX socket connection, and then use the stream_set_blocking () non-blocking mode request:

$fp = Fsockopen ("www.example.com", $errno, $errstr, +), if (! $fp) {die    (' Error fsockopen ');} Convert to non-blocking mode 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);

3 Using CURL

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/"); curl_multi_add_ Handle ($CMH, $ch 1); Curl_multi_exec ($CMH, $active); echo "end\n";

4 using Gearman/swoole extensions

Gearman is a distributed asynchronous processing framework with PHP extensions that can handle high-volume asynchronous tasks.

Swoole is very hot recently, there are many asynchronous methods, easy to use.

5 Using caches and queues

Use caching, queues such as Redis, write data to the cache, and use background scheduling tasks to implement asynchronous data processing.

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

6 Calling system commands

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 '

7 Using Pcntl_fork ()

Installing the pcntl extension, using pcntl_fork () to generate child processes to perform tasks asynchronously, is most convenient for individuals, but also prone to zombie processes.

$pid = Pcntl_fork () if ($pid = = 0) {    child_func ();    Child process function, main process run} else {    father_func ();   The 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);}

8 Native PHP Support

The foreigner's big recruit, did not understand

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

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.