Swoole 2.1 正式版發布,協程+通道帶來全新的 PHP 編程模式

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

PHP的非同步、並行、高效能網路通訊引擎 Swoole 發行 2.1.0 版本。新版本提供了全新的短名 API,完整支援了協程(Coroutine)+通道(Channel)特性,為 PHP 語言帶來了全新的編程模式。Swoole 2.1API借鑒至Go語言,在此向Go語言開發組致敬。

Coroutine

go(function () {    co::sleep(0.5);    echo "hello";});go("test");go([$object, "method"]);

Channel

$chan = new chan(128);$chan->push(1234);$chan->push(1234.56);$chan->push("hello world");$chan->push(["hello world"]);$chan->push(new stdclass);$chan->push(fopen("test.txt", "r+"));while($chan->pop());

Go語言的chan不同,由於PHP是動態語言,所以可以向通道內投遞任意類型的變數。

Channel Select

$c1 = new chan(3);$c2 = new chan(2);$c3 = new chan(2);$c4 = new chan(2);$c3->push(3);$c3->push(3.1415);$c4->push(3);$c4->push(3.1415);go(function () use ($c1, $c2, $c3, $c4) {    echo "select\n";    for ($i = 0; $i < 1; $i++)    {        $read_list = [$c1, $c2];        $write_list = [$c3, $c4];        // $write_list = null;        $result = chan::select($read_list, $write_list, 5);        var_dump($result, $read_list, $write_list);        foreach($read_list as $ch)        {            var_dump($ch->pop());        }        foreach($write_list as $ch)        {            var_dump($ch->push(666));        }        echo "exit\n";    }});go(function () use ($c3, $c4) {    echo "producer\n";    co::sleep(1);    $data = $c3->pop();    echo "pop[1]\n";    var_dump($data);});go(function () {    co::sleep(10);});go(function () use ($c1, $c2) {    co::sleep(1);    $c1->push("resume");    $c2->push("hello");});

MySQL Client

go(function () {    $db = new Co\MySQL();    $server = array(        'host' => '127.0.0.1',        'user' => 'root',        'password' => 'root',        'database' => 'test',    );    $db->connect($server);    $result = $db->query('SELECT * FROM userinfo WHERE id = 3');    var_dump($result);});

Redis Client

go(function () {    $redis = new Co\Redis;    $res = $redis->connect('127.0.0.1', 6379);    $ret = $redis->set('key', 'value');    var_dump($redis->get('key'));});

Http Client

go(function () {    $http = new Co\Http\Client("www.google.com", 443, true);    $http->setHeaders(function () {            });    $ret = $http->get('/');    var_dump($http->body);});

Http2 Client

go(function () {    $http = new Co\Http2\Client("www.google.com", 443, true);    $req = new co\Http2\Request;    $req->path = "/index.html";    $req->headers = [        'host' => "www.google.com",        "user-agent" => 'Chrome/49.0.2587.3',        'accept' => 'text/html,application/xhtml+xml,application/xml',        'accept-encoding' => 'gzip',    ];    $req->cookies = ['name' => 'rango', 'email' => 'rango@swoole.com'];    $ret = $http->send($req);    var_dump($http->recv());});

其他 API

co::sleep(100);co::fread($fp);co::fwrite($fp, "hello world");co::gethostbyname('www.google.com');

伺服器端

$server = new Co\Http\Server('127.0.0.1', 9501);$server->on('Request', function($request, $response) {    $http = new Co\Http\Client("www.google.com", 443, true);    $http->setHeaders(function () {        "X-Power-By" => "Swoole/2.1.0",    });    $ret = $http->get('/');     if ($ret) {        $response->end($http->body);    }    else{        $response->end("recv failed error : {$http->errCode}");    }});$server->start();

Swoole提供了很多Co\ServerCo\WebSocket\ServerCo\Http\ServerCo\Redis\Server4個支援協程的Server類,可以在這些伺服器程式中使用協程API

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.