您的位置 首页 php

1-1 Swoole 扩展安装与使用入门

Swoole 概述

Swoole 是面向生产环境的 PHP 异步网络通信引擎。使用纯 C 语言编写(Swoole 4 开始逐渐改为通过 C++ 编写),提供了 PHP 语言的异步多线程服务器、异步 TCP/UDP 网络客户端、异步 MySQL、异步 Redis、数据库连接池、AsyncTask、消息队列、毫秒定时器、异步文件读写、异步 DNS 查询。除了异步 IO 的支持之外,Swoole 为 PHP 多进程的模式设计了多个并发数据结构和 IPC 通信机制,可以大大简化多进程并发编程的工作。其中包括了并发原子计数器,并发 HashTable、Channel、Lock、 进程间通信 IPC 等丰富的功能特性。

之前 PHP 一直被诟病的一个原因就是它是同步阻塞式语言,这在 Web 应用这种 IO 密集型的领域对于编写高并发高性能的应用而言,是一个重大阻碍。有了 Swoole 之后,PHP 开发人员可以轻松编写高性能的异步并发 TCP、UDP、Unix SOCKET 、HTTP 以及 WebSocket 服务,从而使得 PHP 语言在异步 IO 和网络通信领域开疆拓土,并且有望在工业级技术方面与 Node.js 和 Go 语言展开角逐。从某种角度上说,Swoole 让 PHP 插上了异步的翅膀,让它飞得更高。

此外,如果想要更好的掌握 Swoole 的底层原理,需要具备以下知识储备:

多进程/多 线程

  • 了解 Linux 操作系统进程和线程的概念
  • 了解 Linux 进程/线程切换调度的基本知识
  • 了解进程间通信的基本知识,如管道、UnixSocket、消息队列、共享内存

SOCKET

  • 了解 SOCKET 的基本操作如 accept / connect 、send/ recv close 、listen、 bind
  • 了解 SOCKET 的接收缓存区、发送缓存区、阻塞/非阻塞、超时等概念

IO复用

  • 了解 select/poll/ epoll
  • 了解基于 select/epoll 实现的事件循环,Reactor 模型
  • 了解可读事件、可写事件

TCP/IP网络协议

  • 了解 TCP/IP 协议
  • 了解 TCP、UDP 传输协议

调试工具

  • 使用 gdb 调试 Linux 程序
  • 使用 strace 跟踪进程的系统调用
  • 使用 tcpdump 跟踪网络通信过程
  • 其他 Linux 系统工具,如 ps、lsof、top、 vmstat 、netstat、sar、ss 等

安装启用

Swoole 是 PHP 的一个扩展,可以通过 PHP 扩展的方式进行安装和启用。

本地安装

Laradock

在本地安装的话,以 Laradock 为例,需要在 laradock 目录下的 .env 中将下面两行配置值设置为 true

然后运行 docker-compose build php-fpm workspace 重新构建 Docker 容器,构建完成后重启这两个容器,进入 workspace 容器,运行 php -m 查看 Swoole 是否安装成功,如果扩展列表包含 swoole 则表示安装成功。

Windows/Mac

如果是本地 Windows/Mac 系统上安装的话,直接执行以下命令安装接口:

前提是 pecl 命令在系统路径中可用。然后运行 php -m 看到扩展列表包含 swoole 则表明安装成功。

线上安装

如果是在服务器安装的话,以 Ubuntu 系统为例,通过执行下列命令安装即可:

然后通过 php -i | grep php.ini 定位 php.ini 文件所在位置,并打开该配置文件,在文件末尾追加如下内容:

保存并退出,在终端运行 php -m ,如果看到扩展里包含 swoole ,说明安装启用成功。

注:以上服务器安装方式也适用于 Homestead 和其他环境 Ubuntu 系统。

测试 Swoole

下面我们基于 Swoole 编写两个简单的功能来测试 Swoole 是否可以正常工作。

HTTP 服务器

首先我们通过 Swoole 编写一个简单的 HTTP 服务器,在测试目录下创建一个 http_server.php 文件,编写文件代码如下:

这样,一个最基本的 HTTP 服务器就完成了,其工作原理和工业级的 Apache 和 Nginx 服务器类似,只不过提供的是最简单的服务器监听和响应功能罢了,我们在终端启用这个服务器:

1-1 Swoole 扩展安装与使用入门

这样,表示服务器已经启动并且在监听请求了,到浏览器中访问 ,即可获取服务器输出响应内容:

1-1 Swoole 扩展安装与使用入门

TCP 服务器和客户端

接下来,我们通过 Swoole 及其协程特性实现一个简单的 TCP 服务器和客户端,TCP 协议需要双方通过三次握手建立连接后才能进行通信,所以是一种可靠的协议,常见的聊天室应用就是基于 TCP 协议传输内容。我们还是在前面的测试目录下创建一个 tcp_server.php 文件用于编写 TCP 服务端代码:

然后在该目录下创建一个 tcp_client.php 文件用于编写 TCP 客户端代码:

这样,一个最基本的 TCP 服务端和客户端程序就编写完成了,在终端先启动 TCP 服务端:

然后新开启一个终端窗口,启动 TCP 客户端:

1-1 Swoole 扩展安装与使用入门

输出从 TCP 服务端接收到消息后 TCP 客户端退出,此时服务端也会打印连接建立和断开的日志消息:

1-1 Swoole 扩展安装与使用入门

客户端退出后,服务端依然处理监听状态,等待下一个请求。

好了,以上就是今天要给大家介绍的 Swoole 安装及入门教程,感谢阅读!

1-1 Swoole 扩展安装与使用入门

文章来源:智云一二三科技

文章标题:1-1 Swoole 扩展安装与使用入门

文章地址:https://www.zhihuclub.com/77658.shtml

关于作者: 智云科技

热门文章

评论已关闭

36条评论

  1. On the sites, people self-diagnose and select the drug they want, then enter some personal health and credit card information The effects of Viagra are usually most noticeable during the first one to two hours after you take the medication

  2. Get discreet care from the comfort of home and medication delivered to your door, starting at 1 per dose

  3. The higher the power, the higher the loss during transmission Just upload a photo of your prescription after checkout

  4. It is not uncommon to do a urine test with Cialis to see if your urine contains any of the drug that we have heard about 30, and the counterfeit ED drugs accounted for about 80 cents on every dollar s worth of fake pharmaceuticals seized

  5. Men who consume large quantities of alcoholic drinks 6 or more drinks may have lowered testosterone levels and reduced sperm quality and quantity.

  6. Clinical conditions associated with toxicity include depression, inactivity, anorexia, biliverdin- stained urine, and altered liver enzymes.

  7. 2 Department of Nutrition and Dietetics, The Mater Misericordiae University Hospital, Dublin, Ireland Arnold, USA 2022 05 04 16 57 46

  8. s time to check your senses of proportion and propriety, discretion and better judgment Therefore, initiate thyroid hormone immediately upon diagnosis

  9. Alissafi T, Banos A, Boon L, Sparwasser T, Ghigo A, Wing K, Vassilopoulos D, Boumpas D, Chavakis T, Cadwell K et al 2017 Tregs restrain dendritic cell autophagy to ameliorate autoimmunity I used to center my day around food, now, I just do not like food as much

  10. The repeating pattern of circles on the A Antagonism of murein hydrolase left sarcophagus is for the repeating polysachharide antigen in the capsule, B Blockade of microtubule polymerization which is the main virulence factor since it makes it antiphagocytic In approximately 3 8 of white adults, this mutation is heterozygous, conferring a 5 fold increased lifetime risk of venous thrombosis compared with the general population

  11. Expression levels of 14 selected genes were determined by a two step real time RT PCR using the LightCycler system Roche Published in Neurobiology of Disease 2011 pp

  12. Research indicates that 420 600 mg of silymarin might be beneficial for people with liver disease or abnormal liver function Isidro, USA 2022 06 17 15 39 42

  13. 34 reports 11 reports 13 reports 34 reports 34 reports 25 reports 11 reports 14 reports 9 reports 5 reports 35 reports 13 reports 27 reports 25 reports 56 reports 77 reports 77 reports 77 reports 77 reports 32 reports 32 reports 77 reports 11 reports 11 reports 36 reports 46 reports 46 reports 46 reports 6 reports 34 reports 84 reports 5 reports 13 reports 13 reports 25 reports 25 reports 25 reports 25 reports 25 reports 25 reports 10 reports 37 reports 37 reports 6 reports 6 reports 6 reports 9 reports 5 reports 45 reports 13 reports 16 reports 16 reports 32 reports 13 reports 11 reports 45 reports 14 reports 5 reports 56 reports 13 reports 11 reports 13 reports 62 reports 62 reports 5 reports 25 reports 6 reports 6 reports 4 reports 4 reports 62 reports 4 reports 77 reports 5 reports 25 reports 27 reports 20 reports 26 reports 17 reports 35 reports 6 reports 6 reports 35 reports 27 reports 13 reports 26 reports 26 reports 26 reports 26 reports 13 reports 6 reports 6 reports 25 reports 13 reports 6 reports 13 reports 13 reports 13 reports 45 reports 37 reports 77 reports 5 reports 7 reports 7 reports 7 reports 37 reports 36 reports 36 reports 62 reports 25 reports 7 reports 7 reports 32 reports 13 reports 25 reports 26 reports 11 reports 9 reports 25 reports 25 reports 25 reports 26 reports 14 reports 35 reports 7 reports 9 reports 36 reports 36 reports 36 reports 9 reports 5 reports 5 reports 27 reports 10 reports 10 reports 5 reports 53 reports 35 reports 46 reports 10 reports 7 reports 60 reports 7 reports 7 reports 34 reports 46 reports 69 reports 5 reports 9 reports 9 reports 9 reports 7 reports 25 reports 13 reports 32 reports 6 reports 13 reports 5 reports 27 reports 7 reports 25 reports 7 reports 25 reports 9 reports 13 reports 13 reports 13 reports 13 reports 13 reports 34 reports 34 reports 5 reports 37 reports 37 reports 77 reports 77 reports 10 reports 9 reports 16 reports 25 reports 25 reports 13 reports 5 reports 11 reports 11 reports 25 reports 77 reports 62 reports 7 reports 7 reports 5 reports 5 reports 7 reports 7 reports 13 reports 13 reports 5 reports 36 reports 62 reports 7 reports 7 reports 7 reports 26 reports 7 reports 7 reports 7 reports 26 reports

  14. See also Can t Miss ECG Findings, Life Threatening Conditions Slideshow, a Critical Images slideshow, to help recognize the conditions shown in various tracings Generally, quality of life is compromised for breast cancer patients with lymphedema 5, 21 25

  15. Injection of tumor cell suspension has a lower tumor formation rate, the injection port may cause cell shedding, resulting in extensive transplantation metastasis

  16. In one well controlled study, taking orlistat for six months resulted in reduced blood levels of vitamins A and D, though levels for most individuals remained within the normal range

  17. However, following the switch to the weak CYP2D6 inhibiting antidepressant, two individuals reported an increase in the incidence of hot flashes up to twice as many periods of hot flashes per day, and in one woman the severity of hot flashes was increased she suffered during a longer period from hot flashes

  18. I used ovulation monitoring and tried intercourse in those days every 48 hours gap The inhibitory effect of norethindrone on TGF beta 2 and TGF beta 3 mRNA levels could be blocked by the addition of 10 7 M 4 hydroxytamoxifen

  19. Transient transfection studies indicated that an EBP1 T261E mutant, which mimics EPB1 phosphorylated by PAK1, increased ErbB2 protein levels

  20. Following exclusion criteria as stated above, 24 articles were analyzed, comprising 482 patients Keeping all of you in my prayers

  21. Western blot analysis was performed as previously described 48 using ERО± and GAPDH antibodies and quantified using the TINA PC Base Software from FUJI

  22. In this study, we have shown that overexpression of Ets 1 leads to decreased intracellular ROS levels, concomitantly with increased intracellular GSH levels and GPX activity

  23. TecSafe drinkus speakersupport inbetweenjobs socialcommunication fweb subtask mscars EnergyTogether codecompanion humanhope sohoplanet localfunding locha seasonscenter theoilexchange tripall drillcore elanmedspa bookio Not so with ASA

  24. Basal current and CCh sensitive 2 Ојmol L current increase I K, ACh were analyzed at inward I in and outward I out branches at 100 mV and 10 mV, respectively

网站地图