今天我们来安装和测试一下php的多并发高性能网络通信扩展,这个扩展是使用c语音开发的,加载到php以后,在php的层面上实现了多并发异步通信,模拟了go语音的很多特性,极大的拓宽了php的应用场景。
直接使用官网上的那句命令就可以,安装swoole时可能会出现错误和卡住不动,多试几次就能成功。
pecl install swoole
要配置php的cli环境和fpm环境的ini文件,把扩展的so加载进去要红包。
视频地址:
httprver.php
$http = new swoole_http_rver("127.0.0.1", 9501);$http->on("start", function ($rver) { echo "swoole http rver is started at http://127.0.0.1:9501\n";});$http->on("request", function ($request, $respon) { $respon->header("content-type", "text/plain"); $respon->end("hello world\n");});$http->start();
注意使用httpclient的时候会有一些错误,首先新版的swoole移除了swoole\http\client,会报类找不到,其次要在协程中执行get方法
httpclient.php
$client = new swoole_client(swoole_sock_tcp, swoole_sock_async);$client->on("connect", function($cli) { $cli->nd("hello world\n");});$client->on("receive", function($cli, $data){ echo "received: {$data}花粉的作用\n";});$client->on("error", function($cli){ echo "connect failed建党一百周年好句好段\n";});$client->on("clo", function($cli){ echo "connection clo\n";});$client->connect("127.0.0.1", 9502, 0.5);
taskrver.php
<?php$rv = new swoole\rver("127.0.0.1", 9502, swoole_ba);$rv->t(array( 'worker_num' => 2, 'task_worker_num' => 4,));$rv->on('receive', function(swoole\rver $rv, $fd, $from_id, $data) { echo "接收数据" . $data . "\n"; $data = trim($data); $task_id = $rv->task($data, 0); $rv->nd($fd, "分发任务,任务id为$生命是什么task_id\n");});$rv->on('task', function (swoole\rver $rv, $task_id, $from_id, $data) { echo "tasker进程接收到数据"; echo "#{$rv->worker_id}\tontask: [pid={$rv->worker_pid}]: task_id=$task_id, data_len=".strlen($data).".".php_eol; $rv->finish($data);});$rv->on('finish', function (swoole\rver $rv, $task_id, $data) { echo "task#$task_id finished, data_len=".strlen($data).php_eol;});$rv->on('workerstart', function($rv, $worker_id) { global $argv; if($worker_id >= $rv->tting['worker_num']) { swoole_t_process_name("php {$argv[0]}: task_worker"); } el { swoole_t_process_name("php {$argv[0]}: worker"); }});$rv->start();
taskclient.php
$client = new swoole_client(swoole_sock_tcp, swoole_sock_async);$client->on("connect", function($cli) { $cli->nd("hello world\n");});$client->on("receive", function($cli, $data){ echo "received: {$data}\n";});$client->on("error", function($cli){ 关于情人节的歌 echo "connect failed\n";});$client->on("clo", function($cli){ echo "connection clo\n";});$client->connect("127.0.0.1", 9502, 0.5);
本文发布于:2023-04-07 17:42:28,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/ff9b95e06e851b51a6564ac8c457f952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:[视频教程] 最新版swoole安装和TASKS功能测试.doc
本文 PDF 下载地址:[视频教程] 最新版swoole安装和TASKS功能测试.pdf
留言与评论(共有 0 条评论) |