swoole首页:https://www.swoole.com/

方法1:使用pecl安装

  pecl install swoole

  注意,php版本必须是7.0以及7.0以上的版本。

方法2:编译源码安装

第一步:下载swoole的源码

  下载源码的时候要注意,swoole2.0及以后版本不再支持PHP5.x

git clone  https://github.com/swoole/swoole-src.git

第二步:进入源码目录,执行phpize命令

第三步:配置php-config的路径

./configure --with-php-config=/usr/local/php/bin/php-config

第四步:将php.ini中被禁止的proc_open、proc_get_status、system、exec、shell_exec这几个函数从其中删除,因为在make时要用到这几个函数。

第五步:make

  注意:如果下载的swoole 2.x,而php版本低于7.0,在这一步会失败,请下载正确源码版本

第六步:make test

第七步检测是否安装成功

php  -m

使用swoole搭建一个http服务器

<?php

    $http = new swoole_http_server("0.0.0.0",9501);
$http->on("request", function($request, $response){
print_r($request);
print_r($response);
}); $http->start();

  向服务器发起请求:

curl 123.123.123.123:9501/aaa/bbb/index.html

  运行结果:

Swoole\Http\Request Object
(
[fd] => 1
[header] => Array
(
[user-agent] => curl/7.29.0
[host] => 123.123.123.123:9501
[accept] => */*
) [server] => Array
(
[request_method] => GET
[request_uri] => /aaa/bbb/index.html
[path_info] => /aaa/bbb/index.html
[request_time] => 1531286716
[request_time_float] => 1531286716.3838
[server_port] => 9501
[remote_port] => 46576
[remote_addr] => 123.123.123.123
[master_time] => 1531286716
[server_protocol] => HTTP/1.1
[server_software] => swoole-http-server
) [request] =>
[cookie] =>
[get] =>
[files] =>
[post] =>
[tmpfiles] =>
)
Swoole\Http\Response Object
(
[fd] => 1
[header] =>
[cookie] =>
[trailer] =>
)

  可以观察一下上面Swoole\Http\Request和Swoole\Http\Response的结构(属性)。

使用http服务器返回数据

<?php
$http = new swoole_http_server("0.0.0.0",9501);
$http->on("request", function($request, $response){
$response->header("Content-Type","text/html;chatset=utf-8");
$response->end("hello world\n");
}); $http->start();

  运行脚本,然后请求服务器:

[root@centos ~]# curl xxx.xxx.xxx.xxx:9501/aaa/bbb/index.html
hello world

  

创建WebSocket服务器

  显示index.html的websocket客户端

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body></body>
<script>
let ws = new WebSocket("ws://ganlixin.cn:9501"); ws.onopen = function(){
console.log("connect success");
} ws.onclose = function(){
console.log("closed")
} ws.onmessage = function(res){
console.log(res.data)
}
</script>
</html>

  

  websocket服务器:

<?php
$ws = new swoole_websocket_server("0.0.0.0",9501); //打开连接
$ws->on("open", function($serv, $request){
echo "connected \n";
//向客户端发送信息
$serv->push($request->fd, "hello world\n");
}); //接收到数据
$ws->on("message", function($serv, $request){
echo "received data\n";
$serv->push($request->fd, "get message from client : {$request->data}");
}); //关闭连接
$ws->on("close", function(){
echo "closed\n";
}); $ws->start();

  

  运行index.html,看控制台,有信息输出:

  

定时器

  面向过程的形式

<?php
//循环定时器
//int swoole_timer_tick(int $ms, callable $callback, mixed $user_param);
$timer_id = swoole_timer_tick(5000, function($timer_id) {
echo "current time " . date("Y-m-d H:i:s",time()) . "\n";
});
echo $timer_id; //清除定时器
//swoole_timer_clear($timer_id);

  面向对象的形式

<?php
//循环定时器
$server = new swoole_server("0.0.0.0", 9501);
$timer_id = $server->tick(1000,function($timer_id){
echo "timer_id : " . $timer_id . " current time " . date("Y-m-d H:i:s",time()) . "\n";
}); //注意,没有$swoole_server->clear();
swoole_timer_clear($timer_id);

  使用定时器,一般都是使用面向过程的形式,因为那样的话,可以不用指定ip以及port。

异步TCP服务器

<?php
//创建TCP服务器,默认是同步阻塞工作模式
$server = new swoole_server("0.0.0.0",9501); //设置异步进程数量
$server->set( [ "task_worker_num" => 4 ]); //接收到请求时
$server->on("receive", function($serv, $data, $fd){
$task_id = $serv->task($data); //生成异步任务id
echo "task id is " . $task_id . "\n";
}); //处理异步任务
$server->on("task", function($serv, $task_id, $from_id, $data){
echo "执行异步任务 task_id : $task_id \n";
$serv->finish("data -> OK");
}); $server->on("finish", function($serv, $task_id, $data){
echo "执行任务{$task_id}完成\n";
}); $server->start();
?>

  多次访问服务器绑定的端口,会看到服务器控制台输出内容如下

[root@centos index]# php index.php
task id is 0
执行异步任务 task_id : 0
执行任务0完成
task id is 1
执行异步任务 task_id : 1
执行任务1完成
task id is 2
执行异步任务 task_id : 2
执行任务2完成
task id is 3
执行异步任务 task_id : 3
执行任务3完成

  

TCP客户端

<?php
//创建tcp客户端
//swoole_client->__construct(int $sock_type, int $is_sync = SWOOLE_SOCK_SYNC, string $key);
$client = new swoole_client(SWOOLE_SOCK_TCP); //连接服务器
//bool $swoole_client->connect(string $host, int $port, float $timeout = 0.5, int $flag = 0)
$client->connect("123.123.123.123", 9501, 5) or die("连接失败"); //发送数据request
//int $swoole_client->send(string $data);
$client->send("hello world"); //接受服务器的response
//string $swoole_client->recv(int $size = 65535, int $flags = 0);
//第二个参数表示是否等到所有数据都到达之后再返回
$res = $client->recv(65535);
echo $res;

  

异步TCP客户端

<?php
//创建一个异步tcp客户端
$client = new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_ASYNC); //只有tcp客户端是异步的时候,可以绑定事件
$client->on("connect", function($cli){
echo "connect\n";
}); $client->on("receive", function($cli, $data){
echo "data : $data \n";
}); $client->on("err", function($cli){
echo "error\n";
}); $client->on("close", function($cli){
echo "closed\n";
}); $client->connect("ganlixin.cn",80,10);
?>

  

创建进程执行指定任务

<?php
//创建一个函数,包含之后创建的进程要干的任务
function doJob(swoole_process $worker){
/*
print_r($worker);
Swoole\Process Object
(
[pipe] => 5
[callback] => doJob
[msgQueueId] =>
[msgQueueKey] =>
[pid] => 22777
[id] =>
)
*/
//执行外部程序
//bool swoole_process->exec(string $execfile, array $args)
$worker->exec('/usr/local/apache/bin/apachectl',array("start"));
} //创建进程,将要做的事情(上面指定的函数)传递给构造方法
$process_1 = new swoole_process("doJob");
$pid = $process_1->start();
//echo $pid,"\n"; $process_2 = new swoole_process("doJob");
$pid = $process_2->start();
//echo $pid,"\n"; //等待进程结束
swoole_process::wait();

  

指定进程事件

<?php
//进程池
$process_pool = array(); //进程数量
$process_num = 4; //创建任务
function doJob(swoole_process $process){
//向管道中写入自己的pid
$process->write("my pid is $process->pid");
echo "pid : $process->pid 已写入信息\n";
$process->callback;
} for ( $i = 0; $i < $process_num; $i++ ) {
$process = new swoole_process("doJob");
$pid = $process->start();
$process_pool[] = $process; //存入进程池
} //向每一个子进程指定需要执行的操作
foreach($process_pool as $process){
swoole_event_add($process->pipe, function($pipe) use ($process){
$data = $process->read();
echo "接收到数据 $data\n";
});
}

  运行:

pid : 25176 已写入信息
接收到数据 my pid is 25176
pid : 25177 已写入信息
接收到数据 my pid is 25177
pid : 25175 已写入信息
接收到数据 my pid is 25175
pid : 25174 已写入信息
接收到数据 my pid is 25174

  

进程队列

<?php
//进程池
$process_pool = array(); //进程数
$process_num = 4; //子进程要执行的任务
function doJob(swoole_process $process){
$recv_data = $process->pop();//默认8192字节
echo "从主进程收到数据 $recv_data\n";
sleep(1);
$process->exit(0);//进程退出
} for ( $i = 0; $i < $process_num; $i++ ) {
$process = new swoole_process("doJob",false,false);
//加入进程池
$process_pool[] = $process;
$process->useQueue();//开启进程队列
$process->start();
} //主进程执行
foreach($process_pool as $process){
//主进程向子进程发送数据
$process->push("hello, I'm your father, your pid is $process->pid\n");
} //等待子进程结束
for ( $i = 0; $i < $process_num; $i++ ) {
//array swoole_process::wait(bool $blocking = true);
$process = swoole_process::wait();
echo "子进程{$process["pid"]}退出\n";
} //销毁进程池
unset($process_pool);
?>

  

  

信号触发

<?php
swoole_process::signal(SIGALRM,function(){
//每次收到SIGALRM信号就执行一次
echo "one"; /*
满足某种条件是,取消定时触发信号
if ( condition ){
swoole_process::alarm(-1);
}
*/
}); //单位是微秒 1秒=1000000微秒
//一秒触发一次alarm信号
swoole_process::alarm(1000000); //上面的代码等同于
//swoole_timer_tick(1000,function(){
// echo "one";
//});

  

互斥锁

<?php
//创建互斥锁
$mutex = new swoole_lock(SWOOLE_MUTEX); $mutex->lock();
echo "父进程加锁\n"; if ( pcntl_fork() > 0){
//主进程执行
sleep(2);
$mutex->unlock();
} else {
//子进程执行
echo "子进程等待父进程解锁\n";
$mutex->lock();
echo "子进程加锁\n";
$mutex->unlock();
exit("子进程退出\n");
} echo "主进程释放锁\n";
unset($mutex);
?>

  运行结果:

[root@centos index]# php mutex.php
父进程加锁
子进程等待父进程解锁
主进程释放锁 子进程加锁
子进程退出

  

DNS查询

  通过传入域名,返回ip。

<?php
swoole_async_dns_lookup("www.swoole.com", function($host, $ip){
echo "{$host} : {$ip}\n";
});
?>

  

异步读取文件

  在PHP中读取文件,如果是大文件,可能需要的时间就长一点,那么就要修改php配置参数。

<?php

    //采取分段读的方式
//bool swoole_async_read(string $filename, mixed $callback, int $size = 8192, int $offset = 0);
swoole_async_read("./mutex.php", function($filename,$content){
echo $content;
},10);

  

  

异步读取文件

  分段写入

<?php

    $content = file_get_contents("beego.tar");

    //分段写入
//swoole_async_write(string $filename, string $content, int $offset = -1, mixed $callback = NULL);
//offset置为-1时,表示追加方式
swoole_async_write("data.tar", $content,1);

  

异步mysql

<?php
$mysql = new swoole_mysql(); $config = array(
"host" => "localhost",
"user" => "root",
"password" => "123456",
"database" => "test",
"charset" => "utf8"
); $mysql->connect($config, function(swoole_mysql $db, $result){
if ($result === FALSE) {
echo "$result->connect_errno , $result->connect_error\n";
exit();
}
echo "数据库连接成功\n"; $sql = "select * from demo"; $db->query($sql, function($link, $result){
if ($result === FALSE) {
echo "执行SQL失败\n";
exit();
}
print_r($result);
//$result保存着结果集
});
});
?>

  运行:

[root@centos index]# php async_mysql.php
数据库连接成功
Array
(
[0] => Array
(
[id] => 1
[name] => aaa
[age] => 10
) [1] => Array
(
[id] => 2
[name] => bbb
[age] => 20
) )

  

  

安装使用swoole的更多相关文章

  1. 安装完 swoole 后出现 PHP Warning: PHP Startup: Unable to load dynamic library 'swoole.so'的解决方法

    安装完 swoole 后出现 PHP Warning:  PHP Startup: Unable to load dynamic library 'swoole.so' (tried: /home/s ...

  2. centos7下安装PHP swoole扩展

    PHP的异步.并行.高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库连接池,AsyncTask,消息队列, ...

  3. Ubuntu 16.04 源码编译安装PHP7+swoole

    备注: Ubuntu 16.04 Server 版安装过程图文详解 Ubuntu16镜像地址: 链接:https://pan.baidu.com/s/1XTVS6BdwPPmSsF-cYF6B7Q 密 ...

  4. brew安装PHP7 swoole

    环境: 系统:mac os High Sierra 10.13.3 php版本:7.0.27_19 1.安装homebrew brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在M ...

  5. centos7.5 ab压力测试安装和swoole压力测试

    Apache Benchmark(简称ab) 是Apache安装包中自带的压力测试工具 ,简单易用 1.ab安装 yum -y install httpd-tools 2.ab参数详解,传送门:htt ...

  6. Linux下swoole的安装配置

    前几天搭建swoole环境,在安装php的swoole扩展时不知道什么原因,提示成功,但是使用的时候不能加载,最后决定重新安装php试试,顺便记录了php的安装过程 wget http://cn2.p ...

  7. linux下安装php的swoole扩展模块(安装后php加载不出来?)

    应开发同事要求,需要安装php的扩展模块swoole.swoole是一种PHP高级Web开发框架,框架不是为了提升网站的性能,而是为了提升网站的开发效率,以最少的性能损耗,换取最大的开发效率. 假设服 ...

  8. mac air中编译安装swoole

    本机php版本, 我的7.3.0 1 下载swoole源码 https://github.com/swoole/swoole-src/releases 我下载的版本是swoole-src-4.3.3. ...

  9. php----------linux下安装php的swoole扩展

    1.首先你已经安装好了php环境,这里就不介绍php环境的安装了.如果你是编译安装记得将php加入环境变量,以便于方便查看扩展是否安装成功. 2.我安装的php环境缺少了要给东西,详细看下图 如果你没 ...

随机推荐

  1. 019_删除链表的倒数第N个节点

    //使用两次遍历 ListNode* removeNthFromEnd(ListNode* head, int n) { if (!head->next) return NULL; ; List ...

  2. node基础—模块系统

    模块的概念 为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块加载系统. 在 Node.js 中,文件和模块是一一对应的(每个文件被视为一个独立的模块),换言之,一个 Node ...

  3. D. Equalize Them All Codeforces Round #550 (Div. 3)

    D. Equalize Them All time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. python获取数据网页数据并创建文件夹保存(基于python3.6)

    from urllib.parse import urljoin import urllib.request from bs4 import BeautifulSoup import os impor ...

  5. public private protected default

    public private protected default 这几个限定访问控制符只是在编译阶段起作用,在运行阶段不起作用 private修饰符 , 本类可以访问 default修饰符 , 本类, ...

  6. SpringCloudStream实战

    Spring Cloud Stream是一个用于构建消息驱动的微服务应用程序的框架.Spring Cloud Stream构建于Spring Boot之上,用于创建独立的生产级Spring应用程序,并 ...

  7. UVA1601-The Morning after Halloween(双向BFS)

    Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec  Problem ...

  8. 爬取伯乐在线文章(五)itemloader

    ItemLoader 在我们执行scrapy爬取字段中,会有大量的CSS或是Xpath代码,当要爬取的网站多了,要维护起来很麻烦,为解决这类问题,我们可以根据scrapy提供的loader机制. 导入 ...

  9. WiFi-ESP8266入门http(3-3)网页认证上网-post请求-ESP8266程序

    第一版 原型系统 连上西电的网 直接发送上网的认证信息 返回认证结果网页 成功上网 #include <ESP8266WiFi.h> #define Use_Serial Serial s ...

  10. linux注释多行

    方法一:使用可视化模块添加实现多行注释 1.打开文件/etc/password进行测试: vim /etc/password 2.进入到视图模式:按ctrl+v 1 root:x:0:0:root:/ ...