Swoole 2.1 Official release, co-op + channel brings new PHP programming mode

Source: Internet
Author: User
Tags php language php programming
This is a creation in Article, where the information may have evolved or changed.

PHP's asynchronous, parallel, high-performance network communications engine Swoole published 2.1.0 versions. The new version provides a new short name API that fully supports the Co-op (coroutine) + channel feature, bringing a new programming model to the PHP language. Swoole 2.1 API Go language, to the Go language development team.

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());

Unlike Go language chan , because PHP it is a dynamic language, you can post any type of variable to the channel.

Channel Select

  $c 1 = new Chan (3), $c 2 = new Chan (2); $c 3 = new Chan (2); $c 4 = new Chan (2); $c 3->push (3); $c 3->pus    H (3.1415), $c 4->push (3), $c 4->push (3.1415), Go (function () use ($c 1, $c 2, $c 3, $c 4) {echo "select\n";        for ($i = 0; $i < 1; $i + +) {$read _list = [$c 1, $c 2];        $write _list = [$c 3, $c 4];        $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 ($c 3, $c 4) {echo "producer\n";    Co::sleep (1);    $data = $c 3->pop ();    echo "pop[1]\n"; Var_dump ($data);}); Go (function () {co::sleep (10);});    Go (function () use ($c 1, $c 2) {co::sleep (1);    $c 1->push ("Resume"); $c 2->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());});

Other APIs

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

Server-side

$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();

SwooleProvides a wide Co\Server range Co\WebSocket\Server Co\Http\Server Co\Redis\Server 4 of classes that support co-processes Server , which can be used in these server programs API .

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.