PHP Multi-process programming (i)

Source: Internet
Author: User

Although in PHP, many processes use less of a comparison. But after all, it might be used. I recently encountered such a problem, the user submits hundreds of URLs, to read out the title in this URL.

Of course, you do not want users to wait too long, 10s should give an answer. However, in itself, you want to get a URL of the title, less to 0.1s, more than a few seconds.

Obviously, using a single thread is not a good way to go.

My first design scenario is this:

1. Provide a simple server with the code I provided earlier: http://www.cnblogs.com/niniwzw/archive/2009/09/27/1575002.html

The purpose of this server is to provide a URL and then read the title. Here, you can read 128 bytes at a time to see if you read the title and stop reading if you read the title.

This can save bandwidth.

2. On the client side, open 100 sockets at the same time to access this server. If the number of URLs provided exceeds 100, run multiple times.

This program, basically can meet the requirements, read relatively fast web pages such as: google.com 100 times, also as long as about 1s. However, by testing, it was found that there was a certain

The probability of being blocked when the link is opened. (sometimes blocking a 1s or so, and then going down open) may open too many links, which can be a big problem.

Of course, this is a poor solution: building the TCP link itself consumes very much. Because of the requirements of reliable and orderly transmission, to maintain a data structure, and the system must also open up a certain cache to the client and server side,

The user caches the data. If you create hundreds of links, you may be taking up a lot of memory. As a system of services, it should be as simple as possible, that is, I asked you to do something, you do well after the results to me.

In general, PHP for multi-process programming, the more common is:

1. To do a lot of network time-consuming operations

2. To do a lot of computing, and the system has multiple CPUs, in order to let users have a faster experience, a task, divided into several small tasks, and finally merged.

Therefore, you should try not to have too many complex logic in the call place, the logic built into the service.

My second design scenario is this:

Also with the above server, just, this server function changed, receive no more than 100 URLs, and then open 100 sub-threads, download title. The final merge is returned to the client.

Specifically how to write this server, in the next section.

This test found that the efficiency is much higher. And it's very stable. Download 100 Google about 0.7s. Basically no more than 1s, and the original scheme, often over 5s (20% probability)

Of course, if such a design is just a very simple solution. If there are a lot of people using your service, surely you can't do that.

PHP to do enterprise-level development, a more complex problem is how to deal with memory. There is also a tendency to use arrays to cause memory to swell dramatically. In general, array processing 100,000 data is already the limit,

In small site development rarely used to read such a large amount of data, if encountered, it is best to solve through the C extension, otherwise, a loss of hundreds of M of memory, 10 people will drag you to death.

PHP Multi-process programming (i)

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.