In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to solve the problem that PHP can not achieve multithreading", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to solve the problem that PHP can not achieve multithreading" this article.
The truth is that in most cases, you don't have to use fork or threads, and you'll get better performance than using fork or thread. Suppose you want to set up a service to check the n servers that are running to make sure they are still running. You might write code like this:
$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);? >
It works fine, but it will take a long time to extend this code to manage a large number of servers until fsockopen () has analyzed the hostname and established a successful connection (or a delay of $timeout seconds).
So we have to abandon this code; we can establish an asynchronous connection-without waiting for fsockopen to return the connection state. PHP still needs to parse hostname (so it's wiser to use ip directly), but it will return as soon as a connection is opened, and then we can connect to the next server.
There are two ways to do this; in PHP5, you can directly replace fsocketopen () with the new stream_socket_client () function. In previous versions of PHP5, you had to do it yourself and solve the problem with the sockets extension. Here are the workarounds in PHP5:
$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";}? >
We use stream_select () to wait for the connection event opened by sockets. Stream_select () calls the system's select (2) function to work: the first three parameters are an array of streams you want to use; you can read, write and get exceptions (for three parameters respectively). Stream_select () can wait for the event to occur by setting the $timeout (seconds) parameter-when the event occurs, the corresponding sockets data will be written to your passed-in parameters.
The following is the implementation of the later version of PHP4.1.0, if you have included sockets (ext/sockets) support when compiling PHP, you can use the code similar to the root, but you need to implement the function of the above streams/filesystem function with the ext/sockets function. The main difference is that we use the following function instead of stream_socket_client () to establish a connection:
/ / 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;}? >
Now replace stream_select () with socket_select (), fread () with socket_read (), fwrite () with socket_write (), and fclose () with socket_close () to execute the script!
The advanced thing about PHP5 is that you can use stream_select () to handle almost all stream. For example, you can use include STDIN to receive keyboard input and save it into an array, and you can also receive data from pipes opened by proc_open ().
These are all the contents of this article entitled "how to solve the problem that PHP can not implement multithreading". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.