PHP中的并发请求

in PHP后端 with 0 comment

PHP中发起并发请求

1.curl_multi_*

<?php
$stime = microtime(true);
// build the individual requests, but do not execute them
$ch_1 = curl_init('http://example.com/time?sleep=1');
$ch_2 = curl_init('http://example.com/time?sleep=2');
curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);

// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);

// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running);

//close the handles
curl_multi_remove_handle($mh, $ch_1);
curl_multi_remove_handle($mh, $ch_2);
curl_multi_close($mh);

// all of our requests are done, we can now access the results
$response_1 = curl_multi_getcontent($ch_1);
$response_2 = curl_multi_getcontent($ch_2);
//echo "$response_1 $response_2"; // output results

$etime = microtime(true);

echo $etime - $stime;

2.guzzle

<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Promise\Utils;

$stime = microtime(true);
$client = new Client();
$promises = [
    '1' => $client->getAsync('http://example.com/time?sleep=1'),
'2' => $client->getAsync('http://example.com/time?sleep=3'),
];

// Wait on all of the requests to complete.
$results = Utils::unwrap($promises);

$etime = microtime(true);
// You can access each result using the key provided to the unwrap
// function.
dump((string)$results['1']->getBody());
dump((string)$results['2']->getBody());

echo $etime - $stime;

3.队列worker(多进程)

这个就是直接先把待请求的地址,统统扔到队列里面去,让worker来执行

4.手动多进程

<?php
$s = microtime(true);

$worker_num = 2;
$i = 0;
$pids = [];

do {
    $i++;
    $pid = pcntl_fork();
    if ($pid === -1) {
        die("can't fork !");
    } elseif ($pid !== 0) // main
    {
        $pids[$pid] = $pid;
    } else // child
    {
        file_get_contents('http://example.com/time?sleep=' . $i);
        exit(0);
    }
} while ($i < $worker_num);

do // main
{
    $pid = pcntl_wait($status);

    unset($pids[$pid]);
} while (count($pids));


$e = microtime(true);
var_dump($e - $s);

5.swoole,协程

<?php
$s = microtime(true);
Co\run(function () {
    go(function (){
         file_get_contents('http://example.com/time?sleep=1');
    });

    go(function (){
        file_get_contents('http://example.com/time?sleep=2');
    });
});
$e = microtime(true);
var_dump($e - $s);
<?php
use function Swoole\Coroutine\run;
use Swoole\Runtime;
use Swoole\Coroutine\WaitGroup;

Runtime::enableCoroutine();

run(function () {
    $s = microtime(true);
    $wg = new WaitGroup();
    $result = [];

    $wg->add();
go(function () use ($wg, &$result) {
$result['x'] = file_get_contents('http://example.com/time?sleep=1');
$wg->done();
});

$wg->add();
go(function () use ($wg, &$result) {
$result['y'] = file_get_contents('http://example.com/time?sleep=2');
$wg->done();
});

//挂起当前协程,等待所有任务完成后恢复
$wg->wait();
$e = microtime(true);
var_dump($e - $s);
var_dump($result);
});

6.fiber >8.1

Comments are closed.