Home php教程 php手册 深入讲解PHP线程并发种类

深入讲解PHP线程并发种类

Jun 13, 2016 am 11:05 AM
php Woolen cloth exist accomplish concurrent how us yes go deep of type thread consider explain

线程是我们在做项目中首要考虑的,在php中怎么实现线程呢,我们这里就看看PHP线程的实现。许多 PHP 开发人员认为,由于标准的 PHP 缺少线程功能,因此实际 PHP 应用程序不可能执行多任务处理。

例如,如果应用程序需要其他 Web 站点的信息,那么在远程检索完成之前它都必须停止。这是错误的!通过本文了解如何使用 stream_select 和 stream_socket_client 实现进程内 PHP 多任务处理。PHP 不支持线程。尽管如此,与前述大多数 PHP 开发人员所相信的想法形成对比的是,PHP 应用程序可以 执行多任务处理。让我们开始尽可能清晰地描述一下 “多任务” 和 “线程” 对于 PHP 编程的意义。

PHP线程并发的种类

首先抛开几个和主题无关的例子。PHP 与多任务或并发的关系十分复杂。在较高层次上,PHP 经常涉及多任务:以多任务方式使用 标准的服务器端 PHP 安装 —— 例如,作为 Apache 模块。换句话说,若干个客户机 —— Web 浏览器 —— 可以同时请求同一个 PHP 解释的页面,而 Web 服务器将差不多同时返回所有这些页面。

一个 Web 页面不会妨碍其他 Web 页面的发送,尽管可能会由于诸如服务器内存或网络带宽之类的受限资源而使它们相互之间略有妨碍。这样,实现并发 的系统级需求可能适合使用基于 PHP 的解决方案。就实现而言,PHP 允许它的管理 Web 服务器负责实现并发。

Ajax 名下的客户端并发近几年来也已成为开发人员关注的焦点。虽然 Ajax 的含义已经变得十分模糊,但是它的一个方面是浏览器显示可以同时执行计算和保留对诸如选择菜单项之类的用户操作的响应。这实际上就是某种 多任务。用 PHP 编码的 Ajax 就是这样 —— 但是不涉及任何特定的 PHP;用于其他语言的 Ajax 框架均以完全相同的方法操作。

只粗略地涉及 PHP 的第三个并发实例是 PHP/TK。PHP/TK 是 PHP 的扩展,用于为核心 PHP 提供可移植图形用户界面(GUI)绑定。PHP/TK 允许用 PHP 编写代码构造桌面 GUI 应用程序。其基于事件的特性将模拟一种易于掌握并且比线程更少出错的并发形式。此外,并发是 “继承” 自一项辅助技术,而不是 PHP 的基本功能。

向 PHP 本身添加线程支持的试验已经做过多次。据我所知,没有一次是成功的。但是,Ajax 框架和 PHP/TK 的面向事件的实现表明事件可能比线程能更好地体现 PHP 的并发。PHP V5 证明事实确实如此。使用标准的 PHP V4 和更低版本,必须按顺序执行 PHP 应用程序的所有工作。例如,如果程序需要在两个商业站点检索商品的价格,则请求第一个站点的价格,等待至响应到达,再请求第二个站点的价格,然后再次等待。如果程序请求同时完成若干项任务会怎么样?总体来看,程序将在一段时间内完成,在这段时间内,将始终进行连续处理。

第一个示例PHP线程新的 stream_select 函数及它的几个助手使这成为可能。请考虑以下示例。

清单 1. 同时请求多个 HTTP 页面

<ol class="dp-xml">
<li class="alt"><span><span><?php  </span></span></li>
<li class=""><span>echo "Program starts at ". date('h:i:s') . ".n";  </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>$</span><span class="attribute">timeout</span><span>=</span><span class="attribute-value">10</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">result</span><span>=</span><span class="attribute-value">array</span><span>();  </span>
</li>
<li class="">
<span>$</span><span class="attribute">sockets</span><span>=</span><span class="attribute-value">array</span><span>();  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">convenient_read_block</span><span>=</span><span class="attribute-value">8192</span><span>;  </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/* Issue all requests simultaneously; there's no blocking. */  </span></li>
<li class="">
<span>$</span><span class="attribute">delay</span><span>=</span><span class="attribute-value">15</span><span>;  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">id</span><span>=</span><span class="attribute-value">0</span><span>;  </span>
</li>
<li class=""><span>while ($delay > 0) {  </span></li>
<li class="alt">
<span>$</span><span class="attribute">s</span><span>=</span><span class="attribute-value">stream_socket_client</span><span>("phaseit.net:80", $errno,  </span>
</li>
<li class=""><span>$errstr, $timeout,  </span></li>
<li class="alt"><span>STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);  </span></li>
<li class=""><span>if ($s) {  </span></li>
<li class="alt"><span>$sockets[$id++]=$s;  </span></li>
<li class="">
<span>$</span><span class="attribute">http_message</span><span>=</span><span class="attribute-value">"GET /demonstration/delay?delay="</span><span> .  </span>
</li>
<li class="alt"><span>$delay . " HTTP/1.0rnHost: phaseit.netrnrn";  </span></li>
<li class=""><span>fwrite($s, $http_message);  </span></li>
<li class="alt"><span>} else {  </span></li>
<li class=""><span>echo "Stream " . $id . " failed to open correctly.";  </span></li>
<li class="alt"><span>}  </span></li>
<li class="">
<span>$delay </span><span class="attribute">-</span><span>= </span><span class="attribute-value">3</span><span>;  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>while (count($sockets)) {  </span></li>
<li class="">
<span>$</span><span class="attribute">read</span><span>=$sockets;  </span>
</li>
<li class="alt">
<span>stream_select($read, $</span><span class="attribute">w</span><span>=</span><span class="attribute-value">null</span><span>, $</span><span class="attribute">e</span><span>=</span><span class="attribute-value">null</span><span>, $timeout);  </span>
</li>
<li class=""><span>if (count($read)) {  </span></li>
<li class="alt"><span>/* stream_select generally shuffles $read, so we need to  </span></li>
<li class=""><span>compute from which socket(s) we're reading. */  </span></li>
<li class="alt"><span>foreach ($read as $r) {  </span></li>
<li class="">
<span>$</span><span class="attribute">id</span><span>=</span><span class="attribute-value">array_search</span><span>($r, $sockets);  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">data</span><span>=</span><span class="attribute-value">fread</span><span>($r, $convenient_read_block);  </span>
</li>
<li class=""><span>/* A socket is readable either because it has  </span></li>
<li class="alt"><span>data to read, OR because it's at EOF. */  </span></li>
<li class=""><span>if (strlen($data) == 0) {  </span></li>
<li class="alt"><span>echo "Stream " . $id . " closes at " . date('h:i:s') . ".n";  </span></li>
<li class=""><span>fclose($r);  </span></li>
<li class="alt"><span>unset($sockets[$id]);  </span></li>
<li class=""><span>} else {  </span></li>
<li class="alt">
<span>$result[$id] </span><span class="attribute">.</span><span>= $data;  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>} else {  </span></li>
<li class="alt"><span>/* A time-out means that *all* streams have failed  </span></li>
<li class=""><span>to receive a response. */  </span></li>
<li class="alt"><span>echo "Time-out!n";  </span></li>
<li class=""><span>break;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>?>  </span></li>
</ol>
Copy after login

如果运行此清单,您将看到如下所示的输出。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles