您的位置 首页 php

PHP是如何实现多线程编程的?

理解什么是 多线程

线程

使用多线程主要是因为它在执行效率上有很大优势。由于线程是操作系统能够进行调度的最小单位:

  • 一个多线程程序比单线程程序被操作系统调度的概率更大,所以多线程程序一般会比单线程程序更高效;
  • 多线程程序的多个线程可以在多核 CPU 的多个核心同时运行,可以将完全发挥机器多核的优势;

同时对比多进程程序,多线程有以下特点:

  • 线程的创建和切换的系统开销都比进程要小,所以一定程度上会比多进程更高效;
  • 线程天生的共享内存空间,线程间的通信更简单,避免了进程IPC引入新的复杂度。
适用场景

多线程的优化是很多,可是无脑使用多线程并不能提升程序的执行效率,因为线程的创建和销毁、上下文切换、线程同步等也是有性能损耗的,耗费时间可能比顺序执行的代码还多。如:sumSmall是一个从1累加到50000的函数。

PHP是如何实现多线程编程的?

上图是在主线程内执行了三次 sumSmall 和三个线程分别执行 sumSmall ,再将结果同步到一个线程的时间对比,我们会发现只在主线程执行的时间反而更短,三个线程创建、切换、同步的时间远远大过了线程异步执行节省的时间。

而函数 sumLarge 从1累加到5000000,下图同一线程执行三次和三个线程执行的耗时:

PHP是如何实现多线程编程的?

这次,多线程终于有效率优势了。

是否使用多线程还需要根据具体需求而定,一般考虑以下两种情况:

  • I/O 阻塞会使操作系统发生任务调度,阻塞当前任务,所以代码中 I/O 多的情况下,使用多线程时可以将代码并行。例如多次读整块的文件,或请求多个网络资源。
  • 多线程能充分利用 CPU,所以有多处大计算量代码时,也可以使用多线程使他们并行执行,例如上文中后一个例子。

PHP中的多线程

PHP 默认并不支持多线程,要使用多线程需要安装 pthread 扩展,而要安装 pthread 扩展,必须使用 –enable-maintainer-zts 参数重新编译 PHP,这个参数是指定编译 PHP 时使用 线程安全 方式。

线程安全

多线程是让程序变得不安分的一个因素,在使用多线程之前,首先要考虑线程安全问题:

线程安全:线程安全是编程中的术语,指某个函数、函数库在多线程环境中被调用时,能够正确地处理多个线程之间的共享变量,使程序功能正确完成。

在传统多线程中,由于多个线程共享变量,所以可能会导致出现如下问题:

1 存在一个全局数组$arr = array(‘a’);2 A 线程获取数组长度为1;3 B 线程获取数组长度为1;4 A 线程 pop 出数组元素$a = array_pop($arr); $a = ‘a’;5 B 线程也 pop 数组元素 $b = array_pop($arr); $a = null;6 此时 B 线程内就出现了灵异事件,明明数组长度大于0,或没有pop出东西;

PHP 实现

PHP 实现的线程安全主要是使用TSRM机制对 全局变量 静态变量 进行了隔离,将全局变量和静态变量 给每个线程都复制了一份,各线程使用的都是主线程的一个备份,从而避免了变量冲突,也就不会出现线程安全问题。

PHP 对多线程的封装保证了线程安全,程序员不用考虑对全局变量加各种锁来避免读写冲突了,同时也减少了出错的机会,写出的代码更加安全。

但由此导致的是,子线程一旦开始运行,主线程便无法再对子线程运行细节进行调整了,线程一定程度上失去了线程之间通过全局变量进行消息传递的能力。

同时 PHP 开启线程安全选项后,使用 TSRM 机制分配和使用变量时也会有额外的损耗,所以在不需要多线程的 PHP 环境中,使用 PHP 的 ZTS (非线程安全) 版本就好。

类和方法

PHP 将线程 封装成了 Thread 类,线程的创建通过实例化一个线程对象来实现,由于类的封装性,变量的使用只能通过构造函数传入,而线程运算结果也需要通过类变量传出。

下面介绍几个常用的 Thread 类方法:

  • run():此方法是一个抽象方法,每个线程都要实现此方法,线程开始运行后,此方法中的代码会自动执行;
  • start():在主线程内调用此方法以开始运行一个线程;
  • join():各个线程相对于主线程都是异步执行,调用此方法会等待线程执行结束;
  • kill():强制线程结束;
  • isRunning():返回线程的运行状态,线程正在执行run()方法的代码时会返回 true;

因为线程安全的实现,PHP 的多线程开始运行后,无法再通过共享内存空间通信,线程也无法通过线程间通信复用,所以我认为 PHP 的“线程池”并没有什么意义。扩展内自带的Pool 类是一个对多线程分配管理的类,这里也不再多介绍了。

实例代码

下面是一个线程类,用来请求某一接口。接下来根据它写两个多线程的应用实例:

 class  Request  extends Thread {
    public $url;
    public $response;
    public  function  __construct($url) {
        $this->url = $url;
    }
    public function run() {
        $this->response = file_get_contents($this->url);
    }
}  
异步请求

将同步的请求拆分为多个线程异步调用,以提升程序的运行效率。

 $chG = new Request("www.google.com");
$chB = new Request("www.baidu.com");
$chG ->start();
$chB ->start();
$chG->join();
$chB->join();

$gl = $chG->response;
$bd = $chB->response;  
超时控制

偶然间发现公司网站某一网页上的一块内容时有时无,不知道具体实现,但这给了我使用多线程的灵感:利用线程异步实现快速失败和超时控制。

我们在使用 curl 请求某个地址时,可以通过 CURLOPT_CONNECTTIMEOUT / CURLOPT_TIMEOUT 参数分别设置curl 的连接超时时间和读取数据超时时间,但总的超时时间不好控制。而且在进行数据库查询时的超时时间无法设置。

这时我们便可以借用多线程来实现此功能:在执行线程类的 start() 方法后,不调用 join() 方法,使线程一直处于异步状态,不阻塞主线程的执行。

此时主线程相当于旗舰,而各子线程相当于巡航舰,旗舰到达某地后不必要一直等待巡航舰也归来,等待一段时间后离开即可,从而避免巡航舰意外时旗舰白白空等。

代码:

 $chG = new Request("www.google.com");
$chB = new Request("www.baidu.com");
$chG->start();
$chB->start();
$chB->join();
// 此处不对chG执行join方法

sleep(1); // sleep一个能接受的超时时间
$gl = $chG->response;
$bd = $chB->response;
$bd->kill();
if (!$gl) {
    $gl = ""; // 处理异常,或在线程类内给$gl一个默认值
}  

总结

PHP 对多线程进行的封(yan)装(ge),让人用线程用得非常不尽兴。虽然安全,也保持 PHP 简单易用的一贯风格,却无法完全发挥多线程的能力。不过各个语言各有特色和侧重点,也不必强求,爱她就要包容她。

PHP是如何实现多线程编程的?

文章来源:智云一二三科技

文章标题:PHP是如何实现多线程编程的?

文章地址:https://www.zhihuclub.com/79102.shtml

关于作者: 智云科技

热门文章

评论已关闭

31条评论

  1. Tamoxifen HEXAL tamoxifen citrate decreased the incidence of small estrogen receptor positive tumors, but did not alter the incidence of estrogen receptor negative tumors or larger tumors

  2. When the study was initially designed, a sample size of 2, 150 patients was set to provide more than 80 power to detect a 4 difference in disease free survival, assuming the control group had an event rate of 15 at 5 years hazard ratio, 0 2 2 PREVENT Breast Cancer Translational Center of Excellence, Perelman School of Medicine at the University of Pennsylvania, Philadelphia, Pennsylvania

  3. Perspective Physiologic Importance of Short Chain Fatty Acids from Nondigestible Carbohydrate Fermentation WCRF International

  4. A little Leary of having the product shipped to my mailbox, has anyone had any problems with shipment

  5. bimatoprost levofloxacino 500 precio patente The Ecuadoreans have been unable to collect the 18 billionaward because Chevron no longer has any operations in Ecuador Studies have demonstrated greater rates of breast conserving surgery in postmenopausal oestrogen receptor positive breast cancer patients on neoadjuvant hormonal therapy versus neoadjuvant chemotherapy 5

  6. The rationale for this strategy is that 80 of patients who undergo lumpectomy for small primary tumors have no residual disease after lumpectomy or disease that is within 2 cm of the index cancer

  7. Initial findings of the WHI reported that combined HT was not cardioprotective Anna gibRhvHrOSORgSPQHYy 6 27 2022

  8. In the overall cohort, 62 had a history of hypertension, 24 had diabetes mellitus, 14 had undergone previous cardiac surgery including coronary artery bypass graft or valve surgery and 12 had had a prior myocardial infarction

  9. Physicians rated overall cosmesis, breast pain, breast edema, and skin color changes as significantly worse during the first two years of follow up in the group given tamoxifen plus irradiation than in the tamoxifen group; however, by the four year assessment, the differences were no longer significant Cell 103 843 852

  10. Isovolume hypertonic solutes sodium chloride or mannitol in the treatment of refractory posttraumatic intracranial hypertension 2 mL kg 7

  11. Endometrial polyps with hyperplastic changes ranging from simple to atypical can also be seen

  12. For instance, phytoestrogens naturally occurring compounds in certain plants, herbs and seeds are similar in chemical structure to estrogen and produce estrogen like effects malignant fibrous histiocytoma MFH, liposarcoma, malignant peripheral nerve sheath tumor MPNST, chondrosarcoma, fibrosarcoma, myxosarcoma; sebaceous gland carcinoma; small intestine cancer; sweat gland carcinoma; synovioma; testicular cancer e

  13. com Thirteen other players quietly accepted bans for their Biogenesis links but A Rod went on the offensive

  14. Eligible interventions involved lower targets for systolic diastolic blood pressure 135 85 mmHg or less compared with standard targets for blood pressure 140 to 160 90 to 100 mmHg or less

  15. A Clomid Testosterone Booster Dosage For Sale bluish- white light flashed on her body, and everything seemed to slow down in the place covered by this light In patients assigned to tamoxifen, the follow up measurement was recalculated by adding the treatment effect

  16. The specific roles of these authors are articulated in the author contributions section of the paper

  17. HCTZ reduces the ability of the kidneys to retain sodium in the body, causing sodium and water to be removed in the urine avodart ciprofloxacino lidocaina hidrocortisona gotas oticas similares BRUSSELS, Aug 20 Reuters EU authorities banned importsof herring and mackerel from the Faroe Islands on Tuesday andsaid they would prevent some Faroese boats from docking in EUports, escalating tension in a dispute about allegedover- fishing

  18. Brooks SC, Skafar DF Cyclophilin A enables specific HIV 1 Tat palmitoylation and accumulation in uninfected cells

  19. Gold I catalyzed intramolecular C sp3 H functionalization reaction of vinylidenecyclopropane 44 was disclosed for stereoselective synthesis of benzoxepine 45 Scheme 18

  20. When stratified by age 50 years, a BMI 25 kg m 2 in older patients was associated with a shorter breast cancer free interval Log Rank p 0 4a or ОІ cells exposed to hyperglycaemia for 24 h Fig

网站地图