一、绪
PHP异步网络通信引擎
最终编译为so文件作为PHP的扩展
Linux环境
PHP7 swoole2.1 redis
源码安装PHP7 源码安装swoole
https://www.cnblogs.com/cshaptx4869/p/10395744.html
swoole_timer_tick 每隔一定时间执行,毫秒
swoole_timer_after 一定时间后执行,毫秒
swoole_timer_clear 清除定时器
swoole_async_readfile 异步读取文件内容
执行顺序:1、先读取文件 2、再执行下码 3、最后执行回调
读取的文件不能超过4M
swoole_async_read
swoole_async_writefile
执行顺序和 readfile 相同
swoole_async_write
使用场景
请求文章详情页 -> mysql 文章+1 -> 页面数据展示
进程就是正在运行的程序的一个实例。
Table
Table一个基于共享内存和锁实现的超高性能,并发数据结构。用于解决多进程/多线程数据共享和同步加锁问题。
Atomic
Atomic是Swoole底层提供的原子计数操作类,可以方便整数的无锁原子增减。
二、安装
> wget https://github.com/swoole/swoole-src/archive/v4.3.5.tar.gz
> tar xzvf v4.3.5.tar.gz
> cd swoole-src-4.3.5/
> phpize
> ./configure --with-php-config=/usr/local/php71/bin/php-config --enable-http2 --enable-openssl
> make && make install
> echo ‘extension=swoole.so‘ >> /usr/local/php71/lib/php.ini
> systemctl restart php-fpm.service
如果使用hyperf框架修改php.ini配置文件
swoole.use_shortname = ‘Off‘
$ php --ri swoole

三、nginx+swoole配置
erver {
    root /data/wwwroot/;
    server_name local.swoole.com;
    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header X-Real-IP $remote_addr;
        if (!-e $request_filename) {
             proxy_pass http://127.0.0.1:9501;
        }
    }
}
#通过读取$request->header[‘x-real-ip‘]来获取客户端的真实IP
四、手册
https://toxmc.github.io/swoole-cs.github.io/
demo: https://www.helloweba.net/tag/swoole.html
原文:https://www.cnblogs.com/ksy-c/p/12789614.html