PHP Thread concurrency type-PHP Tutorial-php Tutorial

Source: Internet
Author: User
Describes the PHP Thread concurrency types in depth. Thread is our primary consideration in the project. how to implement the thread in php? let's look at the implementation of the PHP thread here. Many PHP developers think that the lack of thread in standard PHP is the first consideration in our project. how to implement the thread in php? let's look at the implementation of the PHP thread here. 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 PHP Thread 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. 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.

In the first example, the new stream_select function of the PHP thread and its assistants make this possible. Consider the following example.

Listing 1. request multiple HTTP pages at the same time

 
 
  1. <?php
  2. echo "Program starts at ". date('h:i:s') . ".n";
  3. $timeout=10;
  4. $result=array();
  5. $sockets=array();
  6. $convenient_read_block=8192;
  7. /* Issue all requests simultaneously; there's no blocking. */
  8. $delay=15;
  9. $id=0;
  10. while ($delay > 0) {
  11. $s=stream_socket_client("phaseit.net:80", $errno,
  12. $errstr, $timeout,
  13. STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
  14. if ($s) {
  15. $sockets[$id++]=$s;
  16. $http_message="GET /demonstration/delay?delay=" .
  17. $delay . " HTTP/1.0rnHost: phaseit.netrnrn";
  18. fwrite($s, $http_message);
  19. } else {
  20. echo "Stream " . $id . " failed to open correctly.";
  21. }
  22. $delay -= 3;
  23. }
  24. while (count($sockets)) {
  25. $read=$sockets;
  26. stream_select($read, $w=null, $e=null, $timeout);
  27. if (count($read)) {
  28. /* stream_select generally shuffles $read, so we need to
  29. compute from which socket(s) we're reading. */
  30. foreach ($read as $r) {
  31. $id=array_search($r, $sockets);
  32. $data=fread($r, $convenient_read_block);
  33. /* A socket is readable either because it has
  34. data to read, OR because it's at EOF. */
  35. if (strlen($data) == 0) {
  36. echo "Stream " . $id . " closes at " . date('h:i:s') . ".n";
  37. fclose($r);
  38. unset($sockets[$id]);
  39. } else {
  40. $result[$id] .= $data;
  41. }
  42. }
  43. } else {
  44. /* A time-out means that *all* streams have failed
  45. to receive a response. */
  46. echo "Time-out!n";
  47. break;
  48. }
  49. }
  50. ?>

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


Bytes. Many PHP developers believe that the standard PHP lacks a line...

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.