The php multi-process feature is available in java first, but php of later versions also supports the multi-process feature. However, after testing, the performance is not as good as that of java (www.phprm.com, next let's take a look at a few articles I 've sorted out about... the php multi-process feature is available in java first, but php of later versions also supports the multi-process feature. However, after testing, the performance is not as good as that of java (www.phprm.com, next let's take a look at several examples of php multi-process, hoping to help you understand multithreading.
The implementation of php multi-process depends on the pcntl extension. when compiling PHP, you can add "-enable-pcntl" or compile it separately.
Note the following three points:
1. the sub-process does not execute the code before fork. it just copies the memory status of the parent process to a new one. Therefore, the personalized settings of the sub-process must be set separately.
2. output redirection: echo is used in the program, or the command line is messy, which affects resolution. you can use ob_start to redirect to the log file. of course, it is better to use log directly, log files in this instance are grouped by process pid.
3. if no code is executed for the parent process, the child process may exit early and become an orphan process.
Demo receiving:
Use 10 sub-processes to process output tasks. The total number of tasks is 1000. then, the total number of tasks is evenly divided into 10 sub-processes. the code is as follows:
0) {}} // task function do_task ($ task_num) {global $ total; $ start = $ total/10 * $ task_num; $ end = $ total/10 * ($ task_num + 1); for (; $ start <$ end; $ start ++) {echo $ task_num. "". $ start. "\ n";} // the sub-process is terminated after the task is executed. of course, you can return to the code part of the main process for relevant operations. Exit ();} // framework code for multi-process control. The code is as follows: declare (ticks = 1); function sigHandler ($ signal) {echo "a child exited \ n";} pcntl_signal (SIGCHLD, sigHandler, false); echo "this is ". posix_getpid (). PHP_EOL; for ($ I = 0; $ I <3; $ I ++) {$ pid = pcntl_fork (); if ($ pid =-1) {echo 'fork failed '. PHP_EOL;} else if ($ pid) {} else {$ pid = posix_getpid (); echo 'child '. $ pid. ''. time (). PHP_EOL; slee P (rand (2, 5); echo 'child '. $ pid. 'done '. time (). PHP_EOL; exit (0) ;}} do {$ pid = pcntl_wait ($ status); echo 'child quit '. $ pid. PHP_EOL;} while ($ pid> 0); echo 'parent done '. PHP_EOL;?>
Example: a PHP multi-thread, For loop, and a PHP code example For capturing Baidu search pages. the code is as follows:
Url = $ url;} public function run () {if ($ url = $ this-> url )) {$ this-> data = model_http_curl_get ($ url) ;}} function model_thread_result_get ($ urls_array) {foreach ($ urls_array as $ key => $ value) {$ thread_array [$ key] = new test_thread_run ($ value ["url"]); $ thread_array [$ key]-> start ();} foreach ($ thread_array as $ thread_array_key => $ thread_array_value) {while ($ thread_array [$ thread_array_key] -> IsRunning () {usleep (10);} if ($ thread_array [$ thread_array_key]-> join ()) {$ variable_data [$ thread_array_key] = $ thread_array [$ thread_array_key]-> data ;}return $ variable_data;} function model_http_curl_get ($ url, $ userAgent = "") {$ userAgent = $ userAgent? $ UserAgent: 'mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2) '; $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url ); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ curl, CURLOPT_TIMEOUT, 5); curl_setopt ($ curl, CURLOPT_USERAGENT, (www. phprm. com) $ userAgent); $ result = curl_exec ($ curl); curl_close ($ curl); return $ result ;}for ($ I = 0; $ I <100; $ I ++) {$ urls_array [] = array ("name" => "baidu", "url" =>" http://www.phprm.com/s?wd= ". Mt_rand (10000,200 00);} $ t = microtime (true); $ result = model_thread_result_get ($ urls_array); $ e = microtime (true); echo "multithreading :". ($ e-$ t ). "\ n"; $ t = microtime (true); foreach ($ urls_array as $ key => $ value) {$ result_new [$ key] = model_http_curl_get ($ value ["url"]) ;}$ e = microtime (true); echo "For loop :". ($ e-$ t ). "\ n";?>
PHP multithreading class, the code is as follows:
* @ Published: 2010-11-2 ** PHP multithread application example: * require_once 'thread. class. php '; * $ thread = new thread (); * $ thread-> addthread ('Action _ log', 'A '); * $ thread-> addthread ('Action _ log', 'B'); * $ thread-> addthread ('Action _ log', 'C '); * $ thread-> runthread (); ** function action_log ($ info) {* $ log = 'Log /'. microtime (). '. log'; * $ txt = $ info. "rnrn ". 'set in '. date ('H: I: S', time ()). (double) microtime (). "Rn"; * $ fp = fopen ($ log, 'w'); * fwrite ($ fp, $ txt); * fclose ($ fp ); *} */class thread {var $ hooks = array (); var $ args = array (); function thread () {} function addthread ($ func) {$ args = array_slice (func_get_args (), 1); $ this-> hooks [] = $ func; $ this-> args [] = $ args; return true ;} function runthread () {if (isset ($ _ GET ['flag']) {$ flag = intval ($ _ GET ['flag']);} if ($ flag | $ flag === 0 ){ Call_user_func_array ($ this-> hooks [$ flag], $ this-> args [$ flag]);} else {for ($ I = 0, $ size = count ($ this-> hooks); $ I <$ size; $ I ++) {$ fp = fsockopen ($ _ SERVER ['http _ host'], $ _ SERVER ['server _ port']); if ($ fp) {$ out = "GET {$ _ SERVER ['php _ SELF ']}? Flag = $ I HTTP/1.1rn "; $ out. = "Host: {$ _ SERVER ['http _ host']} rn"; $ out. = "Connection: Closernrn"; fputs ($ fp, $ out); fclose ($ fp) ;}}}}?>
The code is as follows:
$thread = new thread(); $thread->addthread('func1','info1'); $thread->addthread('func2','info2'); $thread->addthread('func3','info3'); $thread->runthread();
Description: addthread is used to add a thread function. The first parameter is the function name. The subsequent parameter (optional) is the parameter passed to the specified function.
Runthread is the function of the execution thread.
In linux, you need to configure and install pthreads.
1. for the extended compilation and installation (Linux), edit the parameter www.phprm.com -- enable-maintainer-zts as required. the code is as follows:
Cd/Data/tgz/php-5.5.1. /configure -- prefix =/Data/apps/php -- with-config-file-path =/Data/apps/php/etc -- with-mysql =/Data/apps/mysql -- with-mysqli =/Data/apps/mysql/bin/mysql_config -- with-iconv-dir -- with-freetype-dir =/Data/apps/libs -- with-jpeg-dir = /Data/apps/libs -- with-png-dir =/Data/apps/libs -- with-zlib -- with-libxml-dir =/usr -- enable-xml -- disable-rpath -- enable-bcmath -- enable-shmop -- enable-sysvsem -- enable-inline-optimization -- with-curl -- enable-mbregex -- enable-fpm -- enable-mbstring -- with-mcrypt =/ data/apps/libs -- with-gd -- enable-gd-native-ttf -- with-openssl -- with-mhash -- enable-pcntl -- enable-sockets -- with-xmlrpc -- enable- zip -- enable-soap -- enable-opcache -- with-pdo-mysql -- enable-maintainer-zts make clean make install unzip pthreads-master.zip cd pthreads-master/Data/apps/php/bin /phpize. /configure -- with-php-config =/Data/apps/php/bin/php-config make install vi/Data/apps/php/etc/php. add the following code to ini:
extension = "pthreads.so"
PHP extensions: https://github.com/krakjoe/pthreads
PHP Manual: http://php.net/manual/zh/book.pthreads.php
Article URL:
Reprint ^ at will, but please attach the tutorial address.