PHP Non-blocking mode

Source: Internet
Author: User

PHP Non-blocking mode

by Chenyuan on July 31st,//Filed under→php

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
> Geneva, 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", $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 (); $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.

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 ();  Child process function, the main process runs}else{father_func ();   Main process Function} echo "process". Getmypid (). "Get to the end.\n"; Functionfather_func () {echo "Father pid is". Getmypid (). " \ n ";     } functionchild_func () {sleep (6); echo "Child process exit PID is". Getmypid (). "     \ n "; Exit (0); }

PHP Non-blocking mode

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.