netgusto/co
Composer 安装命令:
composer require netgusto/co
包简介
PHP implementation of tj/co
README 文档
README
PHP implementation of tj/co. Uses generator functions and co-routines (php>=5.5.0) to handle asynchronicity in an apparently synchronous manner.
Install
composer require netgusto/co
Example
<?php require('vendor/autoload.php'); use React\EventLoop\Factory as EventLoopFactory, React\Socket\Server as SocketServer, React\Http\Server as HttpServer; $loop = EventLoopFactory::create(); $socket = new SocketServer($loop); $http = new HttpServer($socket); // we hook co to the reactphp eventloop co::setLoop($loop); function someAsynchronousAPI() { return Promise(function($resolve) { setTimeout(function() use(&$resolve) { // asynchronously resolved after 10ms; could be anything async ! $resolve(['one', 'two', 'three', rand()]); }, 10); }); } $gen = function() { $data = (yield someAsynchronousAPI()); # php has suspended on "yield" # until the promise returned by someAsynchronousAPI() was resolved. # Starting from here, we can use $data synchronously ! yield ['message' => 'Hello World from co !', 'asyncdata' => $data]; }; $http->on('request', function($req, $res) use (&$gen) { co::run($gen)->then(function($value) use(&$req, &$res) { $res->writeHead(200, ['Content-Type' => 'application/json']); $res->end(json_encode($value)); }); }); echo "Magic happens on http://localhost:3000\n"; $socket->listen(3000); $loop->run();
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-01