Php multiplexing (multithreading)-PHP source code

Source: Internet
Author: User
Ec (2); I recently studied php multithreading and found that there are few poor Chinese resources. Only a few articles have been transferred and transferred, but the content in this article has limited value. During the search process, I found that an article cited by many foreign websites was well written, so I translated it. Copyright statement: You can reprint it at will. During reprinting, please be sure to mark the original source and author information of the article in the form of a hyperlink and the question of this statement: Suppose you are writing a php application based on multiple servers, ideally, requests are sent to multiple servers at the same time, rather than one server after another. Can it be implemented? Answer: script ec (2); script

I recently studied php multithreading and found that there are few poor Chinese resources. Only a few articles have been transferred and transferred, but the content in this article has limited value. During the search process, I found that an article cited by many foreign websites was well written, so I translated it.

Copyright Disclaimer: You can reprint the document at will. During reprinting, you must mark the original source and author information of the article as hyperlinks and this statement.
Problem:
Suppose you are writing a php application based on multiple servers. Ideally, you can send requests to multiple servers at the same time, instead of one server after another.
Can it be implemented?
Answer:

When someone wants to implement concurrent functions, they usually think of using fork or spawn threads. But when they find that php does not support multithreading, they will probably change their thinking to use some bad languages, for example, perl.

In fact, in most cases, you do not need to use fork or thread, and you will get better performance than using fork or thread.

Suppose you want to create a service to check the running n servers to make sure they are still running normally. You may write the following code:

$ 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.0 rnHost: $ 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 runs well, but it takes a long time to expand the code to manage a large number of servers before fsockopen () analyzes the hostname and establishes a successful connection (or delay $ timeout seconds.
Therefore, we must discard this Code; we can establish an asynchronous connection-no need to wait for fsockopen to return the connection status. PHP still needs to parse the hostname (so it is more wise to directly use the ip address), but it will return immediately after opening a connection, and then we can connect to the next server.
There are two ways to achieve this. In PHP5, you can use the newly added stream_socket_client () function to directly replace fsocketopen ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.