PHP to develop a multitasking application

Source: Internet
Author: User
Tags php programming server memory
Source: http://www.111cn.net/40316/viewspace_15041.html

Many PHP developers believe that because standard PHP lacks threading capabilities, it is impossible for a real PHP application to perform multitasking. For example, if the application requires information from another WEB site, it must stop before the remote retrieval is complete. This is wrong! Learn how to use Stream_select and stream_socket_client to implement process PHP multitasking with this article.

PHP does not support threads. However, in contrast to the idea that most PHP developers believe, PHP applications can perform multitasking. Let's start by describing as clearly as possible what "multitasking" and "threading" mean for PHP programming.

Types of concurrency

First, let's throw away a few examples that have nothing to do with the topic. PHP has a complex relationship with multitasking or concurrency. At a higher level, PHP often involves multitasking: using a standard server-side PHP installation in a multitasking way-for example, as an Apache module. In other words, several clients--web browsers--You can request a page that is interpreted by the same PHP at the same time, and the WEB server will return almost all of those pages at the same time.

A Web page does not interfere with the sending of other web pages, although they can be slightly hampered by limited resources such as server memory or network bandwidth. Thus, implementing concurrent system-level requirements may be appropriate for using a PHP based solution. For implementation, PHP allows its management Web server to implement concurrency.

Ajax-owned clients have also become the focus of developers ' attention in recent years. Although the meaning of Ajax has become blurred, one aspect of it is that the browser display can perform calculations and retain responses to user actions such as select menu items. This is actually some kind of multitasking. This is how AJAX is encoded in PHP--but it doesn't involve any particular PHP; Ajax frameworks for other languages operate in exactly the same way.

The third concurrent instance that only roughly involves PHP is PHP/TK. PHP/TK is an extension of PHP that is used to provide portable graphical user interface (GUI) bindings for core PHP. PHP/TK allows you to write code in PHP to construct a desktop GUI application. Its event-based feature simulates a concurrency form that is easy to master and less error-prone than threads. In addition, concurrency is "inherited" from an assistive technology rather than PHP's basic functionality.

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

PHP V5 will provide Stream_select ()

With standard PHP V4 and lower versions, you must perform all the work of the PHP application sequentially. For example, if a program needs to retrieve the price of a commodity at two commercial sites, ask for the price of the first site, wait until the response arrives, request the price of the second site, and then wait again.

What happens if a program requests to complete several tasks at the same time? Overall, the program will be completed over a period of time, and will always be processed continuously during this period.

First example

The new Stream_select function and several of its assistants make this possible. Please 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.0rnhost:phaseit.netrnrn";
Fwrite ($s, $http _message);
}
<

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.