多線程是java中一個很不錯的東西,很多朋友說在php中不可以使用PHP多線程了,其實那是錯誤的說法,本文就是介紹PHP中多線程的兩個實作方法,有興趣的同學可以看一下。
PHP本身是不是支援多線程的,不過我們可以藉助其他的方法來實現多線程,例如 shell 服務,例如 web 伺服器,本文我們來講講這兩個方法如何實現。需要的朋友可以來參考一下。
多執行緒是java中很不錯的東西,很多朋友說在php中不可以使用PHP多執行緒了,其實那是錯誤的說法PHP多執行緒實作方法和fsockopen函數有關,以下我們來介紹具體實作程式程式碼,有需要了解的同學可參考。
當有人想要實現並發功能時,他們通常會想到用fork或spawn threads,但是當他們發現php不支援多執行緒的時候,大概會轉換思路去用一些不夠好的語言,比如perl。
其實的是大多數情況下,你大可不必使用 fork 或線程,而且你會得到比用 fork 或 thread 更好的效能。
假設你要建立一個服務來檢查正在運作的n台伺服器,以確定他們還在正常運作。你可能會寫下面這樣的程式碼:
程式碼如下:
<?php $hosts = array("host1.sample.com", "host2.sample.com", "host3.sample.com"); $timeout = 15; $status = array(); foreach ($hosts as $host) { $errno = 0; $errstr = ""; $s = fsockopen($host, 80, $errno, $errstr, $timeout); if ($s) { $status[$host] = "Connectedn"; fwrite($s, "HEAD / HTTP/1.0rnHost: $hostrnrn"); do { $data = fread($s, 8192); if (strlen($data) == 0) { break; } $status[$host] .= $data; } while (true); fclose($s); } else { $status[$host] = "Connection failed: $errno $errstrn"; } } print_r($status); ?>
下面是PHP5的解決方法:
程式碼如下:
#
<?php $hosts = array("host1.sample.com", "host2.sample.com", "host3.sample.com"); $timeout = 15; $status = array(); $sockets = array(); /* Initiate connections to all the hosts simultaneously */ foreach ($hosts as $id => $host) { $s = stream_socket_client(" $ $host:80", $errno, $errstr, $timeout, STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); if ($s) { $sockets[$id] = $s; $status[$id] = "in progress"; } else { $status[$id] = "failed, $errno $errstr"; } } /* Now, wait for the results to come back in */ while (count($sockets)) { $read = $write = $sockets; /* This is the magic function - explained below */ $n = stream_select($read, $write, $e = null, $timeout); if ($n > 0) { /* readable sockets either have data for us, or are failed * connection attempts */ foreach ($read as $r) { $id = array_search($r, $sockets); $data = fread($r, 8192); if (strlen($data) == 0) { if ($status[$id] == "in progress") { $status[$id] = "failed to connect"; } fclose($r); unset($sockets[$id]); } else { $status[$id] .= $data; } } /* writeable sockets can accept an HTTP request */ foreach ($write as $w) { $id = array_search($w, $sockets); fwrite($w, "HEAD / HTTP/1.0rnHost: " . $hosts[$id] . "rnrn"); $status[$id] = "waiting for response"; } } else { /* timed out waiting; assume that all hosts associated * with $sockets are faulty */ foreach ($sockets as $id => $s) { $status[$id] = "timed out " . $status[$id]; } break; } } foreach ($hosts as $id => $host) { echo "Host: $hostn"; echo "Status: " . $status[$id] . "nn"; } ?>
程式碼如下:
<?php // This value is correct for Linux, other systems have other values define('EINPROGRESS', 115); function non_blocking_connect($host, $port, &$errno, &$errstr, $timeout) { $ip = gethostbyname($host); $s = socket_create(AF_INET, SOCK_STREAM, 0); if (socket_set_nonblock($s)) { $r = @socket_connect($s, $ip, $port); if ($r || socket_last_error() == EINPROGRESS) { $errno = EINPROGRESS; return $s; } } $errno = socket_last_error($s); $errstr = socket_strerror($errno); socket_close($s); return false; } ?>
現在用socket_select()替換掉stream_select(),用socket_read()替換掉fread(),用socket_write()替換掉fwrite(),用socket_close()替換掉fclose()就可以執行腳本了!
PHP5的先進之處在於,你可以用stream_select()處理幾乎所有的stream-例如你可以透過include STDIN用它接收鍵盤輸入並保存進數組,你也可以接收透過proc_open ()開啟的管道中的數據。
以下來分享一個PHP多執行緒類別
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); } } } } }
總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。
相關推薦:
php實作微信支付之企業付款
php 並發加鎖案例分析
php#排序演算法實例詳解
以上是PHP中多執行緒的實作方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!