php多线程

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

 

pcntl_forkForks 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

$pid  =  pcntl_fork ();
if ( $pid  == - 1 ) {
     die( 'could not fork' );
} else if ( $pid ) {
      // we are the parent
      pcntl_wait ( $status );  //Protect against Zombie children
} else {
      // we are the child
}

?>
 
貌似会创建一个子线程,不知道在我的板子上好不好用,有空试试。

另外附上有个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

   // create the curl handle
   $ch  =  curl_init ();
  
   // setting several options like url, timeout, returntransfer
   curl_setopt ( $ch ,  CURLOPT_URL ,  'http://www.google.com' );
   curl_setopt ( $ch ,  CURLOPT_TIMEOUT ,  30 );
   curl_setopt ( $ch ,  CURLOPT_RETURNTRANSFER ,  true );
  
   // get the content of the url and put it into the output variable (thanks to the returntransfer option)
   $output  =  curl_exec ( $ch );
  
   // echo the output to the screen
   echo  $output ;
  
   // Print the curl info like http response code, content type etc.
   echo  '<pre>' ;
   print_r  ( curl_getinfo ( $ch ));
  echo  '</pre>' ;
  
   // close the curl handle to free system resources
   curl_close ( $ch );

?>
 



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

   // create the multi curl handle
   $mh  =  curl_multi_init ();
   $handles  = array();
  
  for( $i = 0 ; $i < 5 ; $i ++)
  {
     // create a new single curl handle
     $ch  =  curl_init ();
    
     // setting several options like url, timeout, returntransfer
    // simulate multithreading by calling the wait.php script and sleeping for $rand seconds
     curl_setopt ( $ch ,  CURLOPT_URL ,  "http://put your url here/wait.php?seconds=" .( $i + 1 ));
     curl_setopt ( $ch ,  CURLOPT_HEADER ,  0 );
     curl_setopt ( $ch ,  CURLOPT_RETURNTRANSFER ,  true );
     curl_setopt ( $ch ,  CURLOPT_TIMEOUT ,  30 );
    
     // add this handle to the multi handle
     curl_multi_add_handle ( $mh , $ch );
    
     // put the handles in an array to loop this later on
     $handles [] =  $ch ;
  }
  
   // execute the multi handle
   $running = null ;
  do 
  {
     curl_multi_exec ( $mh , $running );
     // added a usleep for 0.25 seconds to reduce load
     usleep  ( 250000 );
  } while ( $running  >  0 );
  
   // get the content of the urls (if there is any)
   for( $i = 0 ; $i < count ( $handles ); $i ++)
  {
     // get the content of the handle
     $output .=  curl_multi_getcontent ( $handles [ $i ]);
    
     // remove the handle from the multi handle
     curl_multi_remove_handle ( $mh , $handles [ $i ]);
  }
  
   // echo the output to the screen
   echo  $output ;
  
   // close the multi curl handle to free system resources
   curl_multi_close ( $mh );
  

?>
 



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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值