ホームページ >バックエンド開発 >PHPチュートリアル >巧妙解决PHP无法实现多线程的问题_PHP
有没有办法在PHP中实现多线程呢?假设你正在写一个基于多台服务器的PHP应用,理想的情况时同时向多台服务器发送请求,而不是一台接一台。可以实现吗?当有人想要实现并发功能时,他们通常会想到用fork或者spawn threads,但是当他们发现PHP不支持多线程的时候,大概会转换思路去用一些不够好的语言,比如Perl。
bitsCN推荐阅读:优秀的PHP开发者是怎样炼成的?
其实的是大多数情况下,你大可不必使用fork或者线程,并且你会得到比用fork或thread更好的性能。假设你要建立一个服务来检查正在运行的n台服务器,以确定他们还在正常运转。你可能会写下面这样的代码:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">hosts</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("host1.sample.com", "host2.sample.com", "host3.sample.com"); </span></span></li> <li> <span>$</span><span class="attribute"><font color="#ff0000">timeout</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">15</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">status</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); </span> </li> <li><span>foreach ($hosts as $host) { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">errno</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li> <span> $</span><span class="attribute"><font color="#ff0000">errstr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">""</font></span><span>; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">s</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">fsockopen</font></span><span>($host, 80, $errno, $errstr, $timeout); </span> </li> <li><span> if ($s) { </span></li> <li class="alt"><span> $status[$host] = "Connectedn"; </span></li> <li><span> fwrite($s, "HEAD / HTTP/1.0rnHost: $hostrnrn"); </span></li> <li class="alt"><span> do { </span></li> <li> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">fread</font></span><span>($s, 8192); </span> </li> <li class="alt"><span> if (strlen($data) == 0) { </span></li> <li><span> break; </span></li> <li class="alt"><span> } </span></li> <li> <span> $status[$host] </span><span class="attribute"><font color="#ff0000">.</font></span><span>= $data; </span> </li> <li class="alt"><span> } </span></li> <li><span> while (true); </span></li> <li class="alt"><span> fclose($s); </span></li> <li><span> } </span></li> <li class="alt"><span> else { </span></li> <li><span> $status[$host] = "Connection failed: $errno $errstrn"; </span></li> <li class="alt"><span> } </span></li> <li><span>} </span></li> <li class="alt"><span>print_r($status); </span></li> <li> <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
它运行的很好,但是在fsockopen()分析完hostname并且建立一个成功的连接(或者延时$timeout秒)之前,扩充这段代码来管理大量服务器将耗费很长时间。
因此我们必须放弃这段代码;我们可以建立异步连接-不需要等待fsockopen返回连接状态。PHP仍然需要解析hostname(所以直接使用ip更加明智),不过将在打开一个连接之后立刻返回,继而我们就可以连接下一台服务器。
有两种方法可以实现;PHP5中可以使用新增的stream_socket_client()函数直接替换掉fsocketopen()。PHP5之前的版本,你需要自己动手,用sockets扩展解决问题。下面是PHP5中的解决方法:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">hosts</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("host1.sample.com", "host2.sample.com", "host3.sample.com"); </span></span></li> <li> <span>$</span><span class="attribute"><font color="#ff0000">timeout</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">15</font></span><span>; </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">status</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); </span> </li> <li> <span>$</span><span class="attribute"><font color="#ff0000">sockets</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); </span> </li> <li class="alt"><span>/* Initiate connections to all the hosts simultaneously */ </span></li> <li> <span>foreach ($hosts as $</span><span class="attribute"><font color="#ff0000">id</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $host) { </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">s</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">stream_socket_client</font></span><span>("$host:80", $errno, $errstr, $timeout, </span> </li> <li><span> STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); </span></li> <li class="alt"><span> if ($s) { </span></li> <li><span> $sockets[$id] = $s; </span></li> <li class="alt"><span> $status[$id] = "in progress"; </span></li> <li><span> } </span></li> <li class="alt"><span> else { $status[$id] = "failed, $errno $errstr"; </span></li> <li><span> } </span></li> <li class="alt"><span>} </span></li> <li><span>/* Now, wait for the results to come back in */ </span></li> <li class="alt"><span> </span></li> <li><span>while (count($sockets)) { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">read</font></span><span> = $</span><span class="attribute"><font color="#ff0000">write</font></span><span> = $sockets; </span> </li> <li><span>/* This is the magic function - explained below */ </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">n</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">stream_select</font></span><span>($read, $write, $</span><span class="attribute"><font color="#ff0000">e</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>, $timeout); </span> </li> <li> <span> if ($n </span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0) { </span> </li> <li class="alt"><span> /* readable sockets either have data for us, or are failed * connection attempts */ </span></li> <li><span> foreach ($read as $r) { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_search</font></span><span>($r, $sockets); </span> </li> <li> <span> $</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">fread</font></span><span>($r, 8192); </span> </li> <li class="alt"><span> if (strlen($data) == 0) { </span></li> <li><span> if ($status[$id] == "in progress") { </span></li> <li class="alt"><span> $status[$id] = "failed to connect"; </span></li> <li><span> } </span></li> <li class="alt"><span> fclose($r); </span></li> <li><span> unset($sockets[$id]); </span></li> <li class="alt"><span> } </span></li> <li><span> else { </span></li> <li class="alt"> <span> $status[$id] </span><span class="attribute"><font color="#ff0000">.</font></span><span>= $data; </span> </li> <li><span> } </span></li> <li class="alt"><span> } </span></li> <li><span>/* writeable sockets can accept an HTTP request */ </span></li> <li class="alt"><span>foreach ($write as $w) { </span></li> <li> <span> $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array_search</font></span><span>($w, $sockets); </span> </li> <li class="alt"><span> fwrite($w, "HEAD / HTTP/1.0rnHost: " </span></li> <li><span> . $hosts[$id] . "rnrn"); </span></li> <li class="alt"><span> $status[$id] = "waiting for response"; </span></li> <li><span> } </span></li> <li class="alt"><span>} </span></li> <li><span>else { </span></li> <li class="alt"><span>/* timed out waiting; assume that all hosts associated * with $sockets are faulty */ </span></li> <li> <span>foreach ($sockets as $</span><span class="attribute"><font color="#ff0000">id</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $s) { </span> </li> <li class="alt"><span> $status[$id] = "timed out " </span></li> <li><span> . $status[$id]; </span></li> <li class="alt"><span> } </span></li> <li><span>break; </span></li> <li class="alt"><span> } </span></li> <li><span>} </span></li> <li class="alt"> <span>foreach ($hosts as $</span><span class="attribute"><font color="#ff0000">id</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $host) { </span> </li> <li><span> echo "Host: $hostn"; echo "Status: " </span></li> <li class="alt"><span> . $status[$id] . "nn"; </span></li> <li><span>} </span></li> <li class="alt"> <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
我们用stream_select()等待sockets打开的连接事件。stream_select()调用系统的select(2)函数来工 作:前面三个参数是你要使用的streams的数组;你可以对其读取,写入和获取异常(分别针对三个参数)。stream_select()可以通过设 置$timeout(秒)参数来等待事件发生-事件发生时,相应的sockets数据将写入你传入的参数。
下面是PHP4.1.0之后版本的实现,如果你已经在编译PHP时包含了sockets(ext/sockets)支持,你可以使用根上面类似的代 码,只是需要将上面的streams/filesystem函数的功能用ext/sockets函数实现。主要的不同在于我们用下面的函数代替 stream_socket_client()来建立连接:
<ol class="dp-xml"> <li class="alt"><span><span>// This value is correct for Linux, other systems have other values </span></span></li> <li><span>define('EINPROGRESS', 115); </span></li> <li class="alt"><span>function non_blocking_connect($host, $port, &$errno, &$errstr, $timeout) { </span></li> <li> <span> $</span><span class="attribute"><font color="#ff0000">ip</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">gethostbyname</font></span><span>($host); </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">s</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">socket_create</font></span><span>(AF_INET, SOCK_STREAM, 0); </span> </li> <li><span> if (socket_set_nonblock($s)) { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">r</font></span><span> = @socket_connect($s, $ip, $port); </span> </li> <li><span> if ($r || socket_last_error() == EINPROGRESS) { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">errno</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">EINPROGRESS</font></span><span>; </span> </li> <li><span> return $s; </span></li> <li class="alt"><span> } </span></li> <li><span> } </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">errno</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">socket_last_error</font></span><span>($s); </span> </li> <li> <span> $</span><span class="attribute"><font color="#ff0000">errstr</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">socket_strerror</font></span><span>($errno); </span> </li> <li class="alt"><span> socket_close($s); </span></li> <li><span> return false; </span></li> <li class="alt"><span>} </span></li> <li> <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
现在用socket_select()替换掉stream_select(),用socket_read()替换掉fread(),用socket_write()替换掉fwrite(),用socket_close()替换掉fclose()就可以执行脚本了!
PHP5的先进之处在于,你可以用stream_select()处理几乎所有的stream。例如你可以通过include STDIN用它接收键盘输入并保存进数组,你还可以接收通过proc_open()打开的管道中的数据。
原文地址:http://blog.csdn.net/bluephper/archive/2010/01/13/5184861.aspx