Chinaunix首页 | 论坛 | 博客
  • 博客访问: 438670
  • 博文数量: 161
  • 博客积分: 5005
  • 博客等级: 上校
  • 技术积分: 1090
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-20 16:38
文章分类

全部博文(161)

文章存档

2011年(21)

2010年(33)

2009年(89)

2008年(18)

我的朋友

分类:

2010-08-29 20:44:11

一直没有找到PHP有像JAVA一样的多线程机制,网上有的也只是使用get&post模拟出来的多线程。今天,偶尔看到PHP 4 >= 4.1.0, PHP 5有这个函数:

 

pcntl_fork — Forks the currently running process

Description

int pcntl_fork void )

The pcntl_fork() function creates a child process that differs from the parent process only in its PID and PPID. Please see your system's fork(2) man page for specific details as to how fork works on your system.

Return Values

On success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and a PHP error is raised.

 

Php代码 
  1.   
  2. $pid  =  pcntl_fork ();  
  3. if ( $pid  == - 1 ) {  
  4.      die'could not fork' );  
  5. else if ( $pid ) {  
  6.       // we are the parent  
  7.       pcntl_wait ( $status );  //Protect against Zombie children  
  8. else {  
  9.       // we are the child  
  10. }  
  11.   
  12. ?>  
 
貌似会创建一个子线程,不知道在我的板子上好不好用,有空试试。

另外附上有个lady写的,CURL的多线程程序:
Most PHP developers have heard of the CURL extension for PHP or even used it. However it is mostly used in a basic form: to retrieve content from other websites or (RESTful) webservices. Ofcourse PHP itself offers several functions (like fopen or fsockopen) for getting this content, but they are all very basic. It is easy to run into limitations, for example you might want to define the request method or set another user agent (if you're building a webspider). This is where the curl extension kicks in. It is a separate library that has to be compiled with PHP in order to use it. The Curl extension has many functions and options which offer the developer more flexibility than the standard PHP functions.
Let me show you a simple example of using Curl to get the content of another website.

PHP:

Php代码 
  1.   
  2.    // create the curl handle  
  3.    $ch  =  curl_init ();  
  4.     
  5.    // setting several options like url, timeout, returntransfer  
  6.    curl_setopt ( $ch ,  CURLOPT_URL ,  'http://' );  
  7.    curl_setopt ( $ch ,  CURLOPT_TIMEOUT ,  30 );  
  8.    curl_setopt ( $ch ,  CURLOPT_RETURNTRANSFER ,  true );  
  9.     
  10.    // get the content of the url and put it into the output variable (thanks to the returntransfer option)  
  11.    $output  =  curl_exec ( $ch );  
  12.     
  13.    // echo the output to the screen  
  14.    echo  $output ;  
  15.     
  16.    // Print the curl info like http response code, content type etc.  
  17.    echo  '
    ' ;  
  18.    print_r  ( curl_getinfo ( $ch ));  
  19.   echo  '' ;  
  20.     
  21.    // close the curl handle to free system resources  
  22.    curl_close ( $ch );  
  23.   
  24. ?>  
 



A good tutorial which covers the basics of using curl can be found here .

Besides using curl for getting the content of other websites, it is also possible to use curl for multithreading in PHP. PHP has no native support for multithreading like Java. Each PHP request is a separate thread. There are some workarounds like using pcntl_fork, starting multiple commandline php processes using the exec command or even using ajax. Another possibility is using the Curl library. Besides the basic functions described above Curl offers the "multi" functions for retrieving content from several url's at the same time. Let's take a look at these functions using an example:

PHP:

Php代码 
  1.   
  2.    // create the multi curl handle  
  3.    $mh  =  curl_multi_init ();  
  4.    $handles  = array();  
  5.     
  6.   for$i = 0 ; $i < 5 ; $i ++)  
  7.   {  
  8.      // create a new single curl handle  
  9.      $ch  =  curl_init ();  
  10.       
  11.      // setting several options like url, timeout, returntransfer  
  12.     // simulate multithreading by calling the wait.php script and sleeping for $rand seconds  
  13.      curl_setopt ( $ch ,  CURLOPT_URL ,  "" .( $i + 1 ));  
  14.      curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );  
  15.      curl_setopt ( $ch ,  CURLOPT_RETURNTRANSFER ,  true );  
  16.      curl_setopt ( $ch ,  CURLOPT_TIMEOUT ,  30 );  
  17.       
  18.      // add this handle to the multi handle  
  19.      curl_multi_add_handle ( $mh , $ch );  
  20.       
  21.      // put the handles in an array to loop this later on  
  22.      $handles [] =  $ch ;  
  23.   }  
  24.     
  25.    // execute the multi handle  
  26.    $running = null ;  
  27.   do   
  28.   {  
  29.      curl_multi_exec ( $mh , $running );  
  30.      // added a usleep for 0.25 seconds to reduce load  
  31.      usleep  ( 250000 );  
  32.   } while ( $running  >  0 );  
  33.     
  34.    // get the content of the urls (if there is any)  
  35.    for$i = 0 ; $i < count ( $handles ); $i ++)  
  36.   {  
  37.      // get the content of the handle  
  38.      $output .=  curl_multi_getcontent ( $handles [ $i ]);  
  39.       
  40.      // remove the handle from the multi handle  
  41.      curl_multi_remove_handle ( $mh , $handles [ $i ]);  
  42.   }  
  43.     
  44.    // echo the output to the screen  
  45.    echo  $output ;  
  46.     
  47.    // close the multi curl handle to free system resources  
  48.    curl_multi_close ( $mh );  
  49.     
  50.   
  51. ?>  
 



As you can see in the code example we still use the basic Curl functions from the first example to create each curl handle. These handles are put in an array to use later on to retrieve the results. The wait.php script in this example is a simple PHP script which sleeps for the requested amount of seconds to demonstrate how the handles are parallel executed. 

In theory the above code will take as long as the slowest request, in this case 5 seconds. However when executing too much parallel requests, using Curl can cause some overhead. Also error handling in the separate requests can be difficult because there's not an easy way to communicate with the 'threads'. Despite these problems Curl is a serious option to consider if you want to use multithreading in PHP.

One final note: this is not 'real' multithreading but a way to offload things into separate PHP requests. This will put some extra strain on your webserver, so please take that into account.

最后附件附上一个多线程的Daemon,来自:http://phpmultithreaddaemon.blogspot.com/
  •  (4 KB)
  • 下载次数: 52
阅读(572) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~