Table of Contents
[PHP]swoole_server进程的分工
目录
Swoole简介
Swoole:重新定义PHP
功能展示代码片段
TCP Server
TCP Client
主要进程分析
Master进程
Manager进程
Worker进程
Task进程
进程与事件回调的对应关系
Master进程内的回调函数
Worker进程内的回调函数
Task进程内的回调函数
Manager进程内的回调函数
Home Backend Development PHP Tutorial [PHP]swoole_server几个过程的分工

[PHP]swoole_server几个过程的分工

Jun 13, 2016 pm 12:24 PM
body color font markdown

[PHP]swoole_server几个进程的分工

readme.md—/Users/zjh/Documents/我的文章/[PHP]swoole_server几个进程的分工

[PHP]swoole_server进程的分工


摘要:Swoole是一个PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。强大的功能,由背后若干个分工明确的进程来实现,这里详细介绍下几个进程的分工,以便入门者更快速的理解Swoole框架。


  • 博客: http://www.cnblogs.com/jhzhu
  • 邮箱: jhzhuustc@gmail.com
  • 作者: 知明所以
  • 时间: 2015-08-17

目录

  • [PHP]swoole_server进程的分工
    • 目录
    • Swoole简介
      • Swoole:重新定义PHP
      • 功能展示代码片段
        • TCP Server
        • TCP Client
    • 主要进程分析
      • Master进程
      • Manager进程
      • Worker进程
      • Task进程
    • 进程与事件回调的对应关系
      • Master进程内的回调函数
      • Worker进程内的回调函数
      • Task进程内的回调函数
      • Manager进程内的回调函数

Swoole简介

Swoole官网

Swoole:重新定义PHP

Swoole:PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。Swoole虽然是标准的PHP扩展,实际上与普通的扩展不同。普通的扩展只是提供一个库函数。而swoole扩展在运行后会接管PHP的控制权,进入事件循环。当IO事件发生后,swoole会自动回调指定的PHP函数。

功能展示代码片段

TCP Server

<span class="x">$serv = new swoole_server("127.0.0.1", 9501);</span><span class="x">$serv->set(array(</span><span class="x">    'worker_num' => 8,   //工作进程数量</span><span class="x">    'daemonize' => true, //是否作为守护进程</span><span class="x">));</span><span class="x">$serv->on('connect', function ($serv, $fd){</span><span class="x">    echo "Client:Connect.\n";</span><span class="x">});</span><span class="x">$serv->on('receive', function ($serv, $fd, $from_id, $data) {</span><span class="x">    $serv->send($fd, 'Swoole: '.$data);</span><span class="x">    $serv->close($fd);</span><span class="x">});</span><span class="x">$serv->on('close', function ($serv, $fd) {</span><span class="x">    echo "Client: Close.\n";</span><span class="x">});</span><span class="x">$serv->start();</span>
Copy after login

TCP Client

<span class="x">$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);</span><span class="x">//设置事件回调函数</span><span class="x">$client->on("connect", function($cli) {</span><span class="x">    $cli->send("hello world\n");</span><span class="x">});</span><span class="x">$client->on("receive", function($cli, $data){</span><span class="x">    echo "Received: ".$data."\n";</span><span class="x">});</span><span class="x">$client->on("error", function($cli){</span><span class="x">    echo "Connect failed\n";</span><span class="x">});</span><span class="x">$client->on("close", function($cli){</span><span class="x">    echo "Connection close\n";</span><span class="x">});</span><span class="x">//发起网络连接</span><span class="x">$client->connect('127.0.0.1', 9501, 0.5);</span>
Copy after login

更多代码片段请见swoole官网。

主要进程分析

Master进程

Master进程主要用来保证Swoole框架机制的运行。它会创建几个功能性的线程:

  • Reactor线程:就是真正处理TCP连接,收发数据的线程。swoole的主线程在Accept新的连接后,会将这个连接分配给一个固定的Reactor线程,并由这个线程负责监听此socket。在socket可读时读取数据,并进行协议解析,将请求投递到Worker进程。在socket可写时将数据发送给TCP客户端。
  • Master线程(主线程): 负责:Accept新的连接、UNIX PROXI信号处理、定时器任务。
  • 心跳包检测线程:(略)
  • UDP收包线程:(略)

Manager进程

swoole中Worker/Task进程都是由Manager进程Fork并管理的。

  • 子进程结束运行时,manager进程负责回收此子进程,避免成为僵尸进程。并创建新的子进程
  • 服务器关闭时,manager进程将发送信号给所有子进程,通知子进程关闭服务
  • 服务器reload时,manager进程会逐个关闭/重启子进程

为什么不是Master进程呢,主要原因是Master进程是多线程的,不能安全的执行fork操作。

Worker进程

  • 接受由Reactor线程投递的请求数据包,并执行PHP回调函数处理数据
  • 生成响应数据并发给Reactor线程,由Reactor线程发送给TCP客户端
  • 可以是异步非阻塞模式,也可以是同步阻塞模式
  • Worker以多进程的方式运行

Swoole提供了完善的进程管理机制,当Worker进程异常退出,如发生PHP的致命错误、被其他程序误杀,或达到max_request次数之后正常退出。主进程会重新拉起新的Worker进程。 Worker进程内可以像普通的apache+php或者php-fpm中写代码。不需要像Node.js那样写异步回调的代码。

Task进程

  • 接受由Worker进程通过swoole_server->task/taskwait方法投递的任务
  • 处理任务,并将结果数据返回给Worker进程
  • 完全是同步阻塞模式
  • Task以多进程的方式运行

Task进程的全称是task_worker进程,是一种特殊的worker进程。所以onWorkerStart在task进程中也会被调用。当$worker_id >= $serv->setting['worker_num']时表示这个进程是task_worker,否则,代表此进程是worker进程。

进程与事件回调的对应关系

Master进程内的回调函数

<span class="x">onStart</span><span class="x">onShutdown</span><span class="x">onMasterConnect</span><span class="x">onMasterClose</span><span class="x">onTimer</span>
Copy after login

Worker进程内的回调函数

<span class="x">onWorkerStart</span><span class="x">onWorkerStop</span><span class="x">onConnect</span><span class="x">onClose</span><span class="x">onReceive</span><span class="x">onTimer</span><span class="x">onFinish</span>
Copy after login

Task进程内的回调函数

<span class="x">onTask</span><span class="x">onWorkerStart</span>
Copy after login

Manager进程内的回调函数

<span class="x">onManagerStart</span><span class="x">onManagerStop</span>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Vue3 parses markdown and implements code highlighting How Vue3 parses markdown and implements code highlighting May 20, 2023 pm 04:16 PM

Vue implements the blog front-end and needs to implement markdown parsing. If there is code, it needs to implement code highlighting. There are many markdown parsing libraries for Vue, such as markdown-it, vue-markdown-loader, marked, vue-markdown, etc. These libraries are all very similar. Marked is used here, and highlight.js is used as the code highlighting library. The specific implementation steps are as follows: 1. Install dependent libraries. Open the command window under the vue project and enter the following command npminstallmarked-save//marked to convert markdown into htmlnpmins

How to build a Markdown editor in Python How to build a Markdown editor in Python May 13, 2023 am 09:58 AM

First, make sure you have Python3 and Tkinter installed. Other things we need are tkhtmlview and markdown2. You can install them by running pipinstalltkhtmlviewmarkdown2 or pip3installtkhtmlviewmarkdown2 (if you have multiple Python versions). Now launch your favorite editor or IDE and create a new file (for example www.linuxidc.com.py (I named it linuxidc.com editor)). We'll start by importing the necessary libraries. fromtkinterimport*fro

Let's talk about how to configure Markdown in VScode (with basic syntax) Let's talk about how to configure Markdown in VScode (with basic syntax) Dec 07, 2022 pm 03:40 PM

How to use markdown in VScode? The following article will introduce you to the method of configuring Markdown in VScode, and talk about the basic syntax of Markdown. I hope it will be helpful to you!

Python's Pygame Font module - how to use text and fonts? Python's Pygame Font module - how to use text and fonts? Apr 23, 2023 pm 11:19 PM

Pygame's Font text and font Pygame uses the pygame.font module to create a font object to achieve the purpose of drawing text. Commonly used methods of this module are as follows: Name Description pygame.font.init() Initialize the font module pygame.font.quit() Uninitialize the font module pygame.font.get_init() Check whether the font module has been initialized and return a Boolean value . pygame.font.get_default_font() gets the file name of the default font. Returns the file name of the font in the system pygame.font.get_fonts() gets all

How to use PHP to implement markdown conversion How to use PHP to implement markdown conversion Mar 24, 2023 pm 02:30 PM

As people continue to pursue technology, more and more tools and applications are developed to help people simplify complex tasks. One of them is Markdown, which is a lightweight markup language that converts plain text into HTML-formatted text. This article will introduce how to use PHP to implement Markdown conversion.

How to use Markdown in ThinkPHP6 How to use Markdown in ThinkPHP6 Jun 20, 2023 pm 11:00 PM

In the development of the modern Internet era, document writing has gradually changed from cumbersome HTML tags to the simpler and easier to read and write Markdown syntax. ThinkPHP6 uses a highly flexible template engine and provides convenient Markdown extensions, making it very easy to write and display Markdown files in projects. What is Markdown Markdown is a lightweight markup language that can quickly convert documents written in plain text into HTML for use in

After using this Linux command, my boss directly put my name on the salary increase list. After using this Linux command, my boss directly put my name on the salary increase list. Feb 27, 2024 pm 08:49 PM

Overview In Linux systems, we frequently use the command line to process files and directories. Markdown is a concise markup language for quickly creating and formatting documents. But reading and managing Markdown files may require a large number of commands and parameters, which may be a bit complicated for beginners. At this time, you can use the glow command to simplify the operation. glow is a command line tool designed to simplify rendering Markdown files in the Linux terminal. Its main goal is to provide users with a more intuitive and manageable Markdown file reading experience. glow comes with a user-friendly graphical interface that allows you to view and manage Markdown files more easily. With this interface, you don’t need to remember

A must-have Markdown cheat sheet for programmers! A must-have Markdown cheat sheet for programmers! Feb 16, 2023 am 11:22 AM

This article brings you relevant knowledge about Markdown. The main content is to summarize and share with you a Markdown cheat sheet. Friends who are interested can take a look at it below. I hope it will be helpful to everyone.

See all articles