php协程实现mysql异步_Swoole 高性能高并发 PHP 协程框架 - 文章

Swoole 使 PHP 开发人员可以编写高性能高并发的 TCP、UDP、Unix Socket、HTTP、 WebSocket 等服务,让 PHP 不再局限于 Web 领域。Swoole4 协程的成熟将 PHP 带入了前所未有的时期, 为性能的提升提供了独一无二的可能性。

a8a39e7684c31fd09544e3f33e5b91cf.png

Swoole 可以广泛应用于互联网、移动通信、云计算、 网络游戏、物联网(IOT)、车联网、智能家居等领域。使用 PHP + Swoole 可以使企业 IT 研发团队的效率大大提升,更加专注于开发创新产品。

HTTP Server

//高性能HTTP服务器

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

$http->on("start", function ($server) {

echo "Swoole http server is started at http://127.0.0.1:9501\n";

});

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

$response->header("Content-Type", "text/plain");

$response->end("Hello World\n");

});

$http->start();

WebSocket Server

$server = new Swoole\Websocket\Server("127.0.0.1", 9502);

$server->on('open', function($server, $req) {

echo "connection open: {$req->fd}\n";

});

$server->on('message', function($server, $frame) {

echo "received message: {$frame->data}\n";

$server->push($frame->fd, json_encode(["hello", "world"]));

});

$server->on('close', function($server, $fd) {

echo "connection close: {$fd}\n";

});

$server->start();

TCP Server

$server = new Swoole\Server("127.0.0.1", 9503);

$server->on('connect', function ($server, $fd){

echo "connection open: {$fd}\n";

});

$server->on('receive', function ($server, $fd, $reactor_id, $data) {

$server->send($fd, "Swoole: {$data}");

$server->close($fd);

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
tsf (Tencent Server Framework) 是腾讯公司推出的 PHP 协程方案,基于 Swoole PHP Generator 实现的 Coroutine。Tencent Server Framework 是快速服务器部署框架PHP 现在可以像 Golang 一样用协程实现高并发服务器,同时支持同步阻塞,异步非阻塞回调,协程这 3 种 IO 模型。 主要特性: 基于 PHP,相比 C 开发更高效 基于 Swoole 扩展,强大的异步 IO 支持 PHP coroutine 支持服务器监控器和提供接口 要求: php5.5 Swoole1.7.18 linux,OS X 服务器配置: vim server.ini [server] ;server type:tcp,udp,http type = http ; port listen[] = 12312 ; entrance file root = '/data/web_deployment/serv/test/index.php' ;php start path php = '/usr/local/php/bin/php' [setting] ; worker process num worker_num = 16 ; task process num task_worker_num = 0 ; dispatch mode dispatch_mode = 2 ; daemonize daemonize = 1 ; system log log_file = '/data/log/test.log' 启动服务器: cd /root/tsf/bin/ php swoole testHttpServ start 使用 TCP/UDP/HTTP 客户端:   $tcpReturn=(yield $this->tcpTest());   $udpReturn=(yield $this->udpTest());   $httpReturn=(yield $this->httpTest());   public function tcpTest(){     $ip = '127.0.0.1';     $port = '9905';     $data = 'test';     $timeout = 0.5; //second     yield new Swoole\Client\TCP($ip, $port, $data, $timeout);   }   public function udpTest(){     $ip = '127.0.0.1';     $port = '9905';     $data = 'test';     $timeout = 0.5; //second     yield new Swoole\Client\UDP($ip, $port, $data, $timeout);   }   public function httpTest(){     $url='http://www.qq.com';     $httpRequest= new Swoole\Client\HTTP($url);     $data='testdata';     $header = array(       'Content-Length' => 12345,     );     yield $httpRequest->get($url); //yield $httpRequest->post($path, $data, $header);   }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值