Notepad++Good Luck To You!

swoole 并发协程客户端及多进程使用
浏览: 2319    评论: 0

1:swoole延迟收包实现$http=newSwoole\Http\Server("0.0.0.0",9501);$http->on('request',function($request,$respons...


1:swoole延迟收包实现

$http = new Swoole\Http\Server("0.0.0.0", 9501);

$http->on('request', function ($request, $response) {

    $n = 5;

    for ($i = 0; $i < $n; $i++) {

        $cli = new Swoole\Coroutine\Http\Client('httpbin.org', 80);

        $cli->setHeaders([

            'Host' => "httpbin.org",

            "User-Agent" => 'Chrome/49.0.2587.3',

            'Accept' => 'text/html,application/xhtml+xml,application/xml',

            'Accept-Encoding' => 'gzip',

        ]);

        $cli->set(['timeout' => 2]);

        $cli->setDefer();

        $cli->get('/get');

        $clients[] = $cli;

    }

    for ($i = 0; $i < $n; $i++) {

        $r = $clients [$i]->recv();

        $result[] = $clients[$i]->body;

    }

    $response->end(json_encode($result));

});

$http->start();

2.子协程加chan方式实现

$serv = new Swoole\Http\Server("0.0.0.0", 9501);

$serv->on('request', function ($req, $resp) {

    $chan = new chan(2);

    go(function () use ($chan) {

        $cli = new Swoole\Coroutine\Http\Client('httpbin.org', 80);

        $cli->set(['timeout' => 10]);

        $cli->setHeaders([

            'Host' => "httpbin.org",

            "User-Agent" => 'Chrome/49.0.2587.3',

            'Accept' => 'text/html,application/xhtml+xml,application/xml',

            'Accept-Encoding' => 'gzip',

        ]);

        $ret = $cli->post('/post', ['username' => 'post']);

        $chan->push([0 => $cli->body]);

    });

    go(function () use ($chan) {

        $cli = new Swoole\Coroutine\Http\Client('httpbin.org', 80);

        $cli->set(['timeout' => 10]);

        $cli->setHeaders([

            'Host' => "httpbin.org",

            "User-Agent" => 'Chrome/49.0.2587.3',

            'Accept' => 'text/html,application/xhtml+xml,application/xml',

            'Accept-Encoding' => 'gzip',

        ]);

        $ret = $cli->get('/get?method=get');

        $chan->push([1 => $cli->body]);

    });

    $result = [];

    for ($i = 0; $i < 2; $i++) {

        $result += $chan->pop();

    }

    $resp->end(json_encode($result));

});

$serv->start();

3:SaberGM库实现方式

 $responses = SaberGM::requests([

            ['get','uri' => 'http://httpbin.org/get','data'=>['username'=>'admin']],

            ['get','uri' => 'http://httpbin.org/get','data'=>['username'=>'admin888']],

            ['post','uri' => 'http://httpbin.org/post','data'=>['username'=>'administrator']]

        ]);

        echo "请求数:{$responses->success_num}失败{$responses->error_num},完成时间: {$responses->time}<br>";

        foreach($responses as $item){

            echo json_encode($item->getParsedJsonArray());

        }

4.多进程

<?php

$info = array(

  "sendmail"=>1,

  "mailto"=>"12345@qq.com",

  "sendsms"=>1,

  "smsto"=>"123456"

);

echo "start:".date("Y-m-d H:i:s").PHP_EOL;

$mail_process = new swoole_process('sendMail',true);

$mail_process->start();

$sms_process = new swoole_process('sendSMS',true);

$sms_process->start();

//主进程输出子进程范围内容

echo $mail_process->read();

echo PHP_EOL;

echo $sms_process->read();

echo PHP_EOL;

echo "end:".date("Y-m-d H:i:s").PHP_EOL;

//并行函数

function sendMail(swoole_process $worker){

  global $info;

  if($info['sendmail']==1){

    sleep(2);

    $worker->write("send mail to ".$info['mailto']);

  }

}

function sendSMS(swoole_process $worker){

  global $info;

  if($info['sendmail']==1){

    sleep(2);

    $worker->write("send sms to ".$info['smsto']);

  }

}



<?php


$url_arr = array();

for ($i=0;$i<10;$i++){

  $url_arr[] = "www.baidu.com?wd=".$i;

}

echo "start:".date("Y-m-d H:i:s").PHP_EOL;

$workers = array();

for ($i=0;$i<5;$i++){

  $process = new swoole_process('getContents',true);

  $process->start();

  $process->write($i);

  $workers[] = $process;

}

//主进程数据结果

foreach ($workers as $process){

  echo $process->read();

  echo PHP_EOL;

}

echo "end:".date("Y-m-d H:i:s").PHP_EOL;

function getContents(swoole_process $worker){

  $i = $worker->read();

  global $url_arr;

  $res1 = execCurl($url_arr[($i*2)]);

  $res2 = execCurl($url_arr[($i*2+1)]);

  echo $res1.PHP_EOL.$res2;

}

function execCurl($url){

  sleep(2);

  return "handle ".$url." finished";

}


5.httpserver

<?php

$chan=new Channel();

        go(function () use ($chan) {

            $csp = new \EasySwoole\Component\Csp(2);

            $csp->add('t1', function () {

                $cli = new HttpClient('httpbin.org/get?username=admin');

                $cli->setTimeout(10);

                $cli->setKeepAlive(1);

                $cli->setHeaders([

                    'Host' => "httpbin.org",

                    "User-Agent" => 'Chrome/49.0.2587.3',

                    'Accept' => 'text/html,application/xhtml+xml,application/xml',

                    'Accept-Encoding' => 'gzip',

                ]);

                $ret = $cli->get();

                return $ret->getBody();

            });


            $csp->add('t2', function () {

                $cli = new HttpClient('httpbin.org/get?username=user');

                $cli->setTimeout(10);

                $cli->setKeepAlive(1);

                $cli->setHeaders([

                    'Host' => "httpbin.org",

                    "User-Agent" => 'Chrome/49.0.2587.3',

                    'Accept' => 'text/html,application/xhtml+xml,application/xml',

                    'Accept-Encoding' => 'gzip',

                ]);

                $ret = $cli->get();

                return $ret->getBody();


            });

            $chan->push($csp->exec());

        });


        $this->response()->write(json_encode($chan->pop()));


全文详见:http://xpxw.com/?id=117

TOP


«    2024年10月    »
123456
78910111213
14151617181920
21222324252627
28293031
TOP 搜索
TOP 控制面板
您好,欢迎到访网站!
  查看权限
TOP 最新留言
    TOP 作者列表
    TOP 站点信息
    • 文章总数:163
    • 页面总数:0
    • 分类总数:6
    • 标签总数:20
    • 评论总数:0
    • 浏览总数:361552