设为首页 收藏本站
查看: 501|回复: 0

[经验分享] php多线程

[复制链接]

尚未签到

发表于 2017-3-4 11:29:18 | 显示全部楼层 |阅读模式
  一直没有找到PHP有像JAVA一样的多线程机制,网上有的也只是使用get&post模拟出来的多线程。今天,偶尔看到PHP 4 >= 4.1.0, PHP 5有这个函数:
pcntl_fork— Forks the currently running process

  


Description

intpcntl_fork
( void)

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


  


Return Values

On success, the PID of the child process is returned in theparent's thread of execution, and a 0 is returned in the child'sthread of execution.  On failure, a -1 will be returned in theparent's context, no child process will be created, and a PHPerror 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的多线程程序:

Multithreading in PHP with 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/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-350141-1-1.html 上篇帖子: PHP常用模式 下篇帖子: php中ajax
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表