In-depth tutorials on PHP thread concurrency types _php

Source: Internet
Author: User
Threads are the first thing we think about in the project, how to implement threads in PHP, and we'll look at the implementation of PHP threads here. Many PHP developers believe that because standard PHP lacks threading capabilities, the actual PHP application is unlikely to perform multitasking.

For example, if an application needs information from another WEB site, it must stop until the remote retrieval is complete. This is wrong! Learn how to use Stream_select and stream_socket_client to implement in-process PHP multitasking with this article. PHP does not support threading. However, in contrast to the idea that most PHP developers believe, PHP applications can perform multi-tasking. Let's begin to describe as clearly as possible the meaning of "multitasking" and "threading" for PHP programming.

Types of concurrency for PHP threads

Let's start off with a few examples that are irrelevant to the topic. The relationship between PHP and multitasking or concurrency is complex. 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 client--web browsers can request the same PHP interpretation page at the same time, and the WEB server will return almost all of these pages at the same time.

A Web page does not prevent other Web pages from being sent, although they may be slightly hampered by limited resources such as server memory or network bandwidth. In this way, system-level requirements that implement concurrency may be appropriate for use with PHP-based solutions. In terms of implementation, PHP allows its management Web server to be responsible for concurrency.

The client concurrency in Ajax has also been a focus of attention for developers in recent years. Although the meaning of Ajax has become very vague, one aspect of it is that the browser displays the ability to simultaneously perform calculations and retain responses to user actions such as selecting menu items. This is actually some kind of multitasking. This is the case with PHP-encoded AJAX--but not any specific PHP; The Ajax frameworks for other languages operate in exactly the same way.

The third concurrent instance that only roughly covers PHP is PHP/TK. PHP/TK is an extension of PHP that provides 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 nature simulates a concurrency form that is easy to master and has fewer errors than threads. In addition, concurrency is "inheriting" from an assistive technology, rather than the basic functionality of PHP.

The test of adding threading support to PHP itself has been done many times. As far as I know, no one was successful. However, an event-oriented implementation of the AJAX framework and PHP/TK indicates that events may better reflect PHP concurrency than threads. PHP V5 proves that this is true. With standard PHP V4 and lower versions, all the work of the PHP application must be performed sequentially. For example, if a program needs to retrieve the price of a commodity at two commercial sites, request the price of the first site, wait until the response arrives, request a second site price, 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 time period.

The first example of PHP line assigns's stream_select function and several of its assistants makes this possible. Consider the following example.

Listing 1. Request multiple HTTP pages at the same time

 
 
  1. echo "program starts at". Date (' H:i:s '). ". N";
  2. $ Timeout = Ten ;
  3. $ result = Array ();
  4. $ Sockets = Array ();
  5. $ Convenient_read_block = 8192 ;
  6. /* Issue all requests simultaneously; There ' s no blocking. */
  7. $ Delay = the ;
  8. $ ID = 0 ;
  9. while ($delay > 0) {
  10. $ s = stream_socket_client ("phaseit.net:80", $errno,
  11. $errstr, $timeout,
  12. Stream_client_async_connect| Stream_client_connect);
  13. if ($s) {
  14. $sockets [$id ++]= $s;
  15. $ Http_message = "get/demonstration/delay?delay=" .
  16. $delay. "Http/1.0rnhost:phaseit.netrnrn";
  17. Fwrite ($s, $http _message);
  18. } else {
  19. echo "Stream". $id. "Failed to open correctly.";
  20. }
  21. $delay - = 3 ;
  22. }
  23. while (count ($sockets)) {
  24. $ Read = $sockets;
  25. Stream_select ($read, $w=null, $e= NULL , $timeout);
  26. if (count ($read)) {
  27. /* Stream_select generally shuffles $read, so we need to
  28. Compute from which socket (s) we ' re reading. */
  29. foreach ($read as $r) {
  30. $ ID = Array_search ($r, $sockets);
  31. $ Data = fread ($r, $convenient _read_block);
  32. /* A socket is readable either because it has
  33. Data to read, OR because it's at EOF. */
  34. if (strlen ($data) = = 0) {
  35. echo "Stream". $id. "Closes at". Date (' H:i:s '). ". N";
  36. Fclose ($R);
  37. Unset ($sockets [$id]);
  38. } else {
  39. $result [$id] . = $data;
  40. }
  41. }
  42. } else {
  43. /* A time-out means that *all* streams has failed
  44. To receive a response. */
  45. echo "Time-out!n";
  46. Break
  47. }
  48. }
  49. ? >

If you run this manifest, you will see the output as shown below.


http://www.bkjia.com/PHPjc/446460.html www.bkjia.com true http://www.bkjia.com/PHPjc/446460.html techarticle threads are the first thing we think about in the project, how to implement threads in PHP, and we'll look at the implementation of PHP threads here. Many PHP developers think that because of the standard PHP missing 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.