PHP non-blocking mode
By dust on July 31 st, 2014 // Filed Under → php
PHP is no longer blocked. When PHP is processed as the backend, some long-term processing is required. in order to quickly respond to page requests without making a result return judgment, the following measures can be taken:
1. if you are using the 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 ); |
In this example, the output result shows that the output program start.will be returned. the debug's output viewer cannot receive the result, and the log.txt file can receive three complete completion times.
2. use the non-blocking mode of fsockopen and cUrl to request another URL
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 "; Fwrite ($ fp, $ http ); Fclose ($ fp ); |
Use the curl_multi _ * function in cURL to send asynchronous requests
Help
1 2 3 4 5 6 |
$ Cmh = curl_multi_init (); $ Response = curl_init (); Curl_setopt ($ scheme, CURLOPT_URL, "http: // localhost: 6666/child. php "); Curl_multi_add_handle ($ cmh, $ handle ); Curl_multi_exec ($ cmh, $ active ); Echo "End \ n "; |
3. use Gearman and Swoole extensions
Gearman is a distributed asynchronous processing framework with php extensions that can process large batches of asynchronous tasks;
Swoole is very popular recently and has many asynchronous methods, which are easy to use. (Dust margin note: Said to redefine PHP, NodeJS spray completely. Although the Swoole tool is good, it feels that the extension itself is not comparable with NodeJS)
4. use redis and other caches and queues to write data into the cache and implement asynchronous data processing using scheduled tasks in the background.
This method should be common in common large-traffic architectures.
5. in extreme cases, you can call system commands to send data to backend tasks for execution. in my personal opinion, it is not very efficient.
Help
1 2 |
$ Cmd = 'nohup php./processd. php $ someVar>/dev/null &'; '$ Cmd' |
6. foreign experts did not understand the big tricks. php native support
Http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html
VII. install the pcntl extension and use pcntl_fork to generate sub-processes to execute tasks asynchronously. I personally think it is the most convenient, but zombie process is also prone to occur.
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 (); // subprocess function, which runs the main process } 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 ); } |