Develop multi-task applications with PHPV5

Source: Internet
Author: User
Tags php books stock prices
Developing multi-task applications with PHPV5 and reading multi-task applications with PHPV5, many PHP developers believe that the actual PHP application cannot execute multi-task processing because the standard PHP lacks the thread function. For example, if an application requires information from other Web sites, it must be stopped before the remote retrieval is complete. This is "> <LINKhref =" http :/

Many PHP developers believe that, because the standard PHP lacks the thread function, it is impossible for the actual PHP application to execute multi-task processing. For example, if an application requires information from other Web sites, it must be stopped before the remote retrieval is complete. This is wrong! This article describes how to use stream_select and stream_socket_client to implement multi-task PHP processing in the process.

PHP does not support threads. Even so, in contrast to what most PHP developers believe, PHP applications can perform multitasking. Let's start to describe the meaning of "multi-task" and "thread" for PHP programming as clearly as possible.

Types of concurrency

First, let alone a few examples that are irrelevant to the topic. The relationship between PHP and multitasking or concurrency is very complex. At a high level, PHP often involves multi-tasking: using standard server-side PHP installation in a multi-task manner-for example, as an Apache module. In other words, several clients-Web browsers-can simultaneously request pages interpreted by the same PHP, and the Web server will return almost all these pages at the same time.

A Web page does not affect the sending of other Web pages, although it may slightly affect each other due to limited resources such as server memory or network bandwidth. In this way, the PHP-based solution may be suitable for implementing the concurrent system-level requirements. In terms of implementation, PHP allows its Web server to manage for concurrency.

In recent years, client concurrency under Ajax has become the focus of developers' attention. Although the meaning of Ajax has become very vague, one aspect of Ajax is that the browser displays the ability to execute computing and retain responses to user operations such as selecting menu items. This is actually a type of multitasking. PHP-encoded Ajax is like this-but does not involve any specific PHP; Ajax frameworks for other languages are operated in exactly the same way.

The third concurrent instance that only roughly involves PHP is PHP/TK. PHP/TK is an extension of PHP and is used to provide portable graphical user interface (GUI) binding for core PHP. PHP/TK allows you to write code in PHP to construct a desktop GUI application. The event-based feature simulates a concurrency mode that is easier to master and has fewer errors than threads. In addition, concurrency is a secondary technology that "inherits" from, rather than the basic functions of PHP.

The experiment of adding thread support to PHP itself has been done many times. As far as I know, none of them are successful. However, Ajax framework and PHP/TK event-oriented implementation indicate that the event may better reflect PHP concurrency than the thread. PHP V5 proves that this is true.

PHP V5 will provide stream_select ()

With standard PHP V4 and earlier versions, you must perform all the work of the PHP application in sequence. For example, if the program needs to search for the price of the product on two commercial sites, request the price of the first site, wait until the response arrives, then request the price of the second site, and then wait again.

What if the program requests to complete several tasks at the same time? In general, the program will be completed within a period of time, during which it will be processed continuously.

Example 1

The new stream_select function and its assistants make this possible. Consider the following example.

Listing 1. request multiple HTTP pages at the same time

<? Php
Echo "Program starts at". date ('H: I: s'). ". \ n ";

$ Timeout = 10;
$ Result = array ();
$ Sockets = array ();
$ Convenient_read_block = 8192;

/* Issue all requests simultaneously; there's no blocking .*/
$ Delay = 15;
$ Id = 0;
While ($ delay> 0 ){
$ S = stream_socket_client ("phaseit.net: 80", $ errno,
$ Errstr, $ timeout,
STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT );
If ($ s ){
$ Sockets [$ id ++] = $ s;
$ Http_message = "GET/demonstration/delay? Delay = ".
$ Delay. "HTTP/1.0 \ r \ nHost: phaseit.net \ r \ n ";
Fwrite ($ s, $ http_message );
} Else {
Echo "Stream". $ id. "failed to open correctly .";
}
$ Delay-= 3;
}

While (count ($ sockets )){
$ Read = $ sockets;
Stream_select ($ read, $ w = null, $ e = null, $ timeout );
If (count ($ read )){
/* Stream_select generally shuffles $ read, so we need
Compute from which socket (s) we're re reading .*/
Foreach ($ read as $ r ){
$ Id = array_search ($ r, $ sockets );
$ Data = fread ($ r, $ convenient_read_block );
/* A socket is readable either because it has
Data to read, OR because it's at EOF .*/
If (strlen ($ data) = 0 ){
Echo "Stream". $ id. "closes at". date ('H: I: s'). ". \ n ";
Fclose ($ r );
Unset ($ sockets [$ id]);
} Else {
$ Result [$ id]. = $ data;
}
}
} Else {
/* A time-out means that * all * streams have failed
To receive a response .*/
Echo "Time-out! \ N ";
Break;
}
}
?>

If you run this list, you will see the following output.

Listing 2. typical output from the program in listing 1

Program starts at 02:38:50.
Stream 4 closes at 02:38:53.
Stream 3 closes at 02:38:56.
Stream 2 closes at 02:38:59.
Stream 1 closes at 02:39:02.
Stream 0 closes at 02:39:05.

Understanding how this works is crucial. At a higher level, the first program will send several HTTP requests and receive the pages that the Web server sends to it. Although production applications are likely to find the addresses of several Web servers, such as google.com, yahoo.com, and ask.com, all of their requests will be sent to the enterprise server in Phaseit.net., only to reduce complexity.

The Web page request returns results after the latency (variable), as shown below. If the program sends requests in order, it takes about 15 + 12 + 9 + 6 + 3 (45) seconds to complete. As shown in listing 2, it actually takes 15 seconds to complete. Performance increased by three times.

This may be caused by the new stream_select function of PHP V5. Requests are initiated in a conventional way, the method is to open several stream_socket_client and correspond to the http://phaseit.net/demonstration/delay? Delay = $ DELAY writes GET to each stream_socket_client. If you request this URL through a browser, after a few seconds, you will see:

Starting at Thu Apr 12 15:05:01 UTC 2007.
Stopping at Thu Apr 12 15:05:05 UTC 2007.
4 second delay.

The latency server is implemented as CGI as follows:

Listing 3. latency server implementation

#! /Bin/sh

Echo "Content-type: text/html

<HTML> <HEAD> </HEAD> <BODY>"

Echo "Starting at 'date '."
RR = 'echo $ REQUEST_URI | sed-e's /.*? //''
DELAY = 'echo $ RR | sed-e's/delay = //''
Sleep $ DELAY
Echo "<br> Stopping at 'date '."
Echo "<br> $ DELAY second delay. </body>

Although the special implementation of listing 3 is specific to UNIX ?, But almost all implementations in this article will be well applied to Windows? (Especially for versions later than Windows 98) or UNIX installation of PHP. In particular, listing 1 can be hosted on any operating system. Therefore, Linux? And Mac OS X are both UNIX variants, so all the code here can be run in either of them.

Send requests to the delayed server in the following order.

List 4. process startup sequence

Delay = 15
Delay = 12
Delay = 9
Delay = 6
Delay = 3

Stream_select is used to receive results as quickly as possible. In this case, the execution order is the opposite of the order in which the results are sent. 3 seconds later, the first page is ready for reading. This part of the program also conforms to the conventional PHP-In this example, use fread. Just like in other PHP programs, reading can be done well through fgets.

The processing will continue in the same way. The program stops in stream_select until the data is ready. The important thing is that the program will start to read data as long as any connection has data, regardless of the order. This is a method by which a program processes multiple tasks or concurrently results from multiple requests.

Note that this does not cause any burden on the host CPU. We often encounter such network connection programs that use fread in the while by rapidly increasing the CPU usage to 100%. This is not the case here, because stream_select has the attributes required for immediate response (as long as there is any read information ), however, it will generate negligible CPU load during the wait time between read operations.

Required knowledge about stream_select ()

Such event-based programming is not the most basic. Although listing 1 is simplified to containing only the most basic elements, any encoding that involves callback or coordination as a necessary element of a multitasking application is more unfamiliar than a simple program order. In this case, most of the challenges are concentrated on the $ read array. Note that it is a reference; stream_select returns important information by changing the content of $ read. Just as pointers are the biggest stumbling block to C, referencing seems to be the most tricky part of PHP.

You can use this technology to send requests to any external Web site, so that your program will receive all results as soon as possible without waiting for other requests. In fact, this technology will correctly process all TCP/IP connections, not just the connections on the Web port 80. Therefore, you can manage LDAP retrieval, SMTP transmission, and SOAP requests in general.

But that's not all. PHP V5 manages various connections such as "streams", not just simple sockets. The PHP Client URL library (CURL) supports HTTPS certificates, FTP uploads, and cookies. (CURL allows PHP applications to connect to the server using various protocols ). Since CURL provides a stream interface, the connection is transparent from the program perspective. The next section will show how stream_select transfers local computing.

Pay attention to stream_select. It is still working on documentation, because even the latest PHP books are not related to it. Several code examples that can be obtained on the Web cannot work at all or are confusing. The second and third parameters of stream_select are used to manage the write and exception channels corresponding to the read channel in listing 1 and should always be null. Except for a few exceptions, selecting these two parameters in the writable or abnormal channel is incorrect. Unless you have experience, please stick to the readable choice.

In addition, at least before PHP V5.1.2, stream_select has obvious errors. The most important thing is that you cannot trust the return value of a function. Although I have not yet debugged the implementation, experience tells me that you can safely test the count ($ read) in listing 1, but the return value of the test stream_select itself is not safe (although there is an official document ).

Local PHP concurrency

Examples and most of the above discussions mainly discuss how to manage several remote resources at the same time and receive the results, rather than waiting to process each request in the order of the initial request. This must be an important application of PHP concurrency. The actual application speed can sometimes be increased by 10 times or more.

What if performance declines? Is there a way to speed up the PHP results that are limited by local processing? There are multiple methods. If there is any difference, these methods are not as well-known as socket-oriented methods in listing 1. There are many reasons for this situation, including:

Most PHP pages are fast enough-better performance is an advantage, but it is not worth investing in new code.
Using PHP on a Web page can discard some minor performance improvements-when the only value criterion is the time required to deliver the entire Web page, it is not important to reschedule the calculation to get intermediate results faster.

PHP cannot control local bottlenecks-users may complain about taking 8 seconds to extract details of account records, but it is likely to be a database processing or some other PHP external resource constraints. Even if PHP processing is reduced to zero, it takes more than 7 seconds to simply search.

Even few constraints are parallel-assuming that a specific page will calculate the suggested transaction price for a specific list of common shares, and the calculation is very complex and takes some time. In essence, computing may be executed sequentially. There is no obvious way to divide it into "team collaboration ".

Few PHP programmers are aware of the potential of PHP to achieve concurrency. Among the few who have the need to use parallel performance, most of the people I met all say that PHP "does not support threads" and is willing to use the existing computing model.

However, sometimes we can do better. Assuming that the PHP page needs to calculate the two stock prices, the two may need to be compared, and the underlying host is just a multi-processor. In this case, by allocating two completely different and time-consuming computations to different processors, the performance may be improved by almost twice.

Such instances are rare in all PHP computing fields. However, since I have found no precise records everywhere, I need to include a model for such acceleration.

Listing 5. latency server implementation

<? Php
Echo "Program starts at". date ('H: I: s'). ". \ n ";

$ Timeout = 10;
$ Streams = array ();
$ Handles = array ();

/* First launch a program with a delay of three seconds, then
One which returns after only one second .*/
$ Delay = 3;
For ($ id = 0; $ id <= 1; $ id ++ ){
$ Error_log = "/tmp/error". $ id. ". txt"
$ Descriptorspec = array (
0 => array ("pipe", "r "),
1 => array ("pipe", "w "),
2 => array ("file", $ error_log, "w ")
);
$ Cmd = 'sleep '. $ delay.'; echo "Finished with delay '.
$ Delay .'".';
$ Handles [$ id] = proc_open ($ cmd, $ descriptorspec, $ pipes );
$ Streams [$ id] = $ pipes [1];
$ All_pipes [$ id] = $ pipes;
$ Delay-= 2;
}

While (count ($ streams )){
$ Read = $ streams;
Stream_select ($ read, $ w = null, $ e = null, $ timeout );
Foreach ($ read as $ r ){
$ Id = array_search ($ r, $ strea ** ms );
Echo stream_get_contents ($ all_pipes [$ id] [1]);
If (feof ($ r )){
Fclose ($ all_pipes [$ id] [0]);
Fclose ($ all_pipes [$ id] [1]);
$ Return_value = proc_close ($ handles [$ id]);
Unset ($ streams [$ id]);
}
}
}
?>

This program generates the following output:

Program starts at 10:28:41.
Finished with delay of 1.
Finished with delay of 3.

The key here is that PHP starts two independent sub-processes, retrieves the output of the first process to be completed, and then retrieves the output of the second process, even if the latter is started earlier. If the host is a multi-processor computer and the operating system is correctly configured, the operating system itself is responsible for allocating subprograms to different processors. This is a good way to use PHP in a multi-processor host.

PHP supports multiple tasks. PHP does not support threads according to the methods used by other languages such as Java programming language or C ++. However, the above example shows that PHP has more than imagined acceleration potential.

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.