今天我们来安装和测试一下php的多并发高性能网络通信扩展,这个扩展是使用C语音开发的,加载到PHP以后,在PHP的层面上实现了多并发异步通信,模拟了go语音的很多特性,极大的拓宽了PHP的应用场景。

直接使用官网上的那句命令就可以,安装swoole时可能会出现错误和卡住不动,多试几次就能成功。
pecl install swoole
要配置php的cli环境和fpm环境的ini文件,把扩展的so加载进去。

视频地址:

https://www.bilibili.com/video/av70354024/

httpServer.php

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

注意使用httpClient的时候会有一些错误,首先新版的swoole移除了Swoole\Http\Client,会报类找不到,其次要在协程中执行get方法

httpClient.php

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
$cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
echo "received: {$data}\n";
});
$client->on("error", function($cli){
echo "connect failed\n";
});
$client->on("close", function($cli){
echo "connection close\n";
});
$client->connect("127.0.0.1", 9502, 0.5);

taskServer.php

<?php
$serv = new Swoole\Server("127.0.0.1", 9502, SWOOLE_BASE); $serv->set(array(
'worker_num' => 2,
'task_worker_num' => 4,
)); $serv->on('Receive', function(Swoole\Server $serv, $fd, $from_id, $data) {
echo "接收数据" . $data . "\n";
$data = trim($data);
$task_id = $serv->task($data, 0);
$serv->send($fd, "分发任务,任务id为$task_id\n");
}); $serv->on('Task', function (Swoole\Server $serv, $task_id, $from_id, $data) {
echo "Tasker进程接收到数据";
echo "#{$serv->worker_id}\tonTask: [PID={$serv->worker_pid}]: task_id=$task_id, data_len=".strlen($data).".".PHP_EOL;
$serv->finish($data);
}); $serv->on('Finish', function (Swoole\Server $serv, $task_id, $data) {
echo "Task#$task_id finished, data_len=".strlen($data).PHP_EOL;
}); $serv->on('workerStart', function($serv, $worker_id) {
global $argv;
if($worker_id >= $serv->setting['worker_num']) {
swoole_set_process_name("php {$argv[0]}: task_worker");
} else {
swoole_set_process_name("php {$argv[0]}: worker");
}
}); $serv->start();

  

taskClient.php

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
$cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
echo "received: {$data}\n";
});
$client->on("error", function($cli){
echo "connect failed\n";
});
$client->on("close", function($cli){
echo "connection close\n";
});
$client->connect("127.0.0.1", 9502, 0.5);

  

[视频教程] 最新版swoole安装和TASKS功能测试的更多相关文章

  1. swoole 安装

    swoole 安装: 1. 下载源代码,我下载的是1.8.6版本wget https://github.com/swoole/swoole-src/archive/1.8.6-stable.tar.g ...

  2. adobe cc最新版 软件安装与激活

     adobe cc最新版 软件安装与激活:https://m.weike.fm/lecture/4912961 说明#:Adobe CC2017的所有软件都可以按照以上方法进行安装,如:Premier ...

  3. nginx+php+swoole安装记录

    领了台阿里服务器1vCPU 1G,做下测试研究. 系统 centos7,使用yum安装. Nginx yum install nginx ##开启nginx service nginx start 安 ...

  4. 利用Swoole实现PHP+websocket直播,即使通讯代码,及linux下swoole安装基本配置

    swoole安装基本配置 php安装swoole 1. 下载swoole安装 wget http://pecl.php.net/get/swoole-1.9.1.tgz tar -zxvf swool ...

  5. VMware® Workstation 15 Pro 最新版软件安装教程

    VMware 15 Pro下载地址: https://pan.baidu.com/s/1ILY2PTqB-BaJMn2hbKO4CA 提取码:vebd 如有问题咨询QQ:2217084817 VMwa ...

  6. android studio最新版的安装和配置(3.1.2)

    android studio最新版的安装和配置(3.1.2) 下载地址: android studio:http://www.android-studio.org/ JDK:http://www.or ...

  7. PHP7.2 、git、swoole安装

    一.安装php 1.安装gcc yum -y install gcc gcc-c++ 2.安装一些库 yum -y install php-mcrypt libmcrypt-devel libxml2 ...

  8. [视频教程] ubuntu系统下安装最新版的MySQL

    视频地址: https://www.bilibili.com/video/av69256331/ 官网文档https://dev.mysql.com/doc/mysql-apt-repo-quick- ...

  9. [视频教程] ubuntu系统下安装最新版PHP7.3.X环境

    视频地址: https://www.bilibili.com/video/av69088870/ 笔记: 先安装一下这个命令 add-apt-repositoryapt-get install sof ...

随机推荐

  1. Android App图片资源文件压缩利器McImage

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/195 Android App图片资源文件压缩利器McIma ...

  2. 【Gradle】Android Gradle 高级自定义

    Android Gradle 高级自定义 使用共享库 Android的包,如android.app,android.content,android.view,android.widget等,是默认包含 ...

  3. Xamarin Bindableproperty 可绑定属性

    重要的事情说三遍: 本文基本是取自微软官方 Bindable Properties, 官方也提供了机翻的中文版本,笔者只是尝试用自己的理解描述一遍,便于记忆.如有不对之处,欢迎拍砖. 本文基本是取自微 ...

  4. Ride to Office

    [题目描述] 起点与终点相隔4500米.现Charley需要从起点骑车到终点.但是,他有个习惯,沿途需要有人陪伴,即以相同的速度,与另外一个人一起骑.而当他遇到以更快的速度骑车的人时,他会以相应的速度 ...

  5. 如何获取数据泵dm和dw进程的 Strace (Doc ID 1411563.1)

    How To Get A Strace Of The Data Pump dm And dw Process(es) (Doc ID 1411563.1) APPLIES TO: Oracle Dat ...

  6. Java成员变量和局部变量区别

    成员变量和局部变量区别 变量根据定义位置的不同,我们给变量起了不同的名字.如下图所示: 区别 在类中的位置不同  (重点) 成员变量:类中,方法外 局部变量:方法中或者方法声明上(形式参数) 作用范围 ...

  7. WPF DataGrid 双击行 获得绑定数据

    原文:WPF DataGrid 双击行 获得绑定数据 1)增加事件 2)增加对象获取 1)事件代码 Datagrid 增加事件 MouseDoubleClick="dataGrid_Mous ...

  8. MySQL select from where multiple conditions

    Maybe one of the most used MySQL commands is SELECT, that is the way to stract the information from ...

  9. mysql执行操作时卡死

    有时候使用Navicat对mysql数据库进行添加字段,truncate或其他操作时会一直卡住不动,后来查看进程才发现一直处于等待状态 先执行,列出所有进程 show full processlist ...

  10. tomcat9上传文件失败错误

    项目上线正常运行一段时间后,有一天突然所有的附件上传都出现了错误,查找项目本身的日志系统也一致没有跟踪到错误.经过几番折腾,在tomcat9-stdout.log日志中发现如下异常: ERROR or ...