Home > Backend Development > PHP Problem > How to implement concurrency in php

How to implement concurrency in php

藏色散人
Release: 2023-03-06 15:42:01
Original
5082 people have browsed it

php method to achieve concurrency: first write the request interface for third parties; then use concurrent requests for these third-party interfaces, concurrent code such as "function request(){...}"; finally use php Just implement the loop request interface.

How to implement concurrency in php

Recommended: "PHP Video Tutorial"

php achieves concurrency

Usage Scenarios

If you have multiple third-party interfaces that require cyclic requests, concurrent processing is required at this time, because PHP is a single-process, sequentially executed program. If one interface hangs, then the subsequent The interface cannot get the request, or the waiting time of an interface is too long, and the following interfaces also need waiting time. Then more than ten interfaces wait for 1S each, and ten interfaces take 10S to complete a request. At this time, you need to use concurrent requests

Usage plan

You can first write a request interface for a third party, and then use concurrent requests for these third-party interfaces

Concurrent code

function request(){
$srart_time = microtime(TRUE);
$webSiteUrl = 'http://test/';
/**
 * 结算
 */
$alls = [
    $webSiteUrl . "1.php",
    $webSiteUrl . "2.php",
];
//1 创建批处理cURL句柄
$chHandle = curl_multi_init();
$chArr = [];
//2.创建多个cURL资源
foreach ($alls as $Url) {
    $chArr[$Url] = curl_init();
    curl_setopt($chArr[$Url], CURLOPT_URL, $Url);
    curl_setopt($chArr[$Url], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($chArr[$Url], CURLOPT_TIMEOUT, 1);
    curl_multi_add_handle($chHandle, $chArr[$Url]); //2 增加句柄
}
$active = null;
/**常量
 * CURLM_CALL_MULTI_PERFORM==-1
 * // CURLM_OK == 0
 **/
do {
    $mrc = curl_multi_exec($chHandle, $active); //3 执行批处理句柄
} while ($mrc == CURLM_CALL_MULTI_PERFORM); //4
//4 $active 为true,即$chHandle批处理之中还有$ch句柄正待处理,$mrc==CURLM_OK,即上一次$ch句柄的读取或写入已经执行完毕。
while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($chHandle) != CURLM_CALL_MULTI_PERFORM) {//$chHandle批处理中还有可执行的$ch句柄,curl_multi_select($chHandle) != -1程序退出阻塞状态。
        do {
            $mrc = curl_multi_exec($chHandle, $active);//继续执行需要处理的$ch句柄。
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}
foreach ($chArr as $k => $ch) {
//    $result[$k] = curl_multi_getcontent($ch); //5 获取句柄的返回值,不需要
    curl_multi_remove_handle($chHandle, $ch);//6 将$chHandle中的句柄移除
}
curl_multi_close($chHandle); //7 关闭全部句柄
$end_time = microtime(TRUE);
echo sprintf("use time:%.3f s", $end_time - $srart_time);
}
Copy after login

php implements the loop request interface

This situation is for programs that need to be executed within 1S. If the execution frequency exceeds 1S, just use the Linux scheduled task directly.

// 无时间限制执行此程序
set_time_limit(0);
// 设置默认时区  北京时间
date_default_timezone_set('PRC');
do {
    //  如果不存在 stop  文件,则程序停止 并且记录停止时间
    if (!file_exists(dirname(__FILE__) . '/stop')) {
        $handle = fopen('./ceshi.log', 'a+');
        fwrite($handle, '程序停止时间: ' . date('Y-m-d H:i:s') . "\n");
        fclose($handle);
        exit();
    }
    sleep(4);
    try {
        requestNewOpen();
    } catch (Exception $exception) {
        $handle = fopen('./ceshi.log', 'a+');
        fwrite($handle, 'catch : ' . $exception->getMessage() . "\n");
        fclose($handle);
    }
} while (true);
Copy after login

The above is the detailed content of How to implement concurrency in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template