内容简介:PHP 的异步、并行、高性能网络通信引擎 Swoole 已发布 2.1.0 版本。新版本提供了全新的短名 API,完整支持了协程(Coroutine)+通道(Channel)特性,为 PHP 语言带来了全新的编程模式。Swoole 2.1的API借鉴至G...
PHP 的异步、并行、高性能网络通信引擎 Swoole 已发布 2.1.0 版本。新版本提供了全新的短名 API,完整支持了协程(Coroutine)+通道(Channel)特性,为 PHP 语言带来了全新的编程模式。Swoole 2.1的API借鉴至 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\Server、Co\WebSocket\Server、Co\Http\Server、Co\Redis\Server共4个支持协程的Server类,可以在这些服务器程序中使用协程API。
【声明】文章转载自:开源中国社区 [http://www.oschina.net]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Golang 并发,有缓存通道,通道同步案例演示
- 科普 | 菜鸟学习状态通道,Part-2:App 定制型状态通道
- golang通道定义
- 通道 | Java NIO
- Go 通道
- golang 通道channel 基础
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Types and Programming Languages
Benjamin C. Pierce / The MIT Press / 2002-2-1 / USD 95.00
A type system is a syntactic method for automatically checking the absence of certain erroneous behaviors by classifying program phrases according to the kinds of values they compute. The study of typ......一起来看看 《Types and Programming Languages》 这本书的介绍吧!