PHP Non-blocking mode (go from Chenyuan)

Source: Internet
Author: User
PHP Non-blocking mode (transfer from Chenyuan)

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.

Help

01

02

03

04

05

06

07

08

09

10

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

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

Help

1

2

3

4

5

6

7

8

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

Send an asynchronous request using the Curl_multi_* function in Curl

Help

1

2

3

4

5

6

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

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.

Help

1

2

$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.

Help

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

if (($pid = pcntl_fork()) == 0) {

child_func(); //子进程函数,主进程运行

} else {

father_func(); //主进程函数

}

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

}

  • 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.