[Php]swoole_server Several processes of division of labor

Source: Internet
Author: User
Tags connection pooling php language
<title>readme.md-/users/zjh/documents/my article/[php]swoole_server several processes of division of labor</title>

[Division of the Php]swoole_server process

Absrtact: Swoole is a high performance network communication framework for PHP language, which provides asynchronous multi-threaded server for PHP language, asynchronous TCP/UDP network client, asynchronous MySQL, database connection pool, Asynctask, message queue, MS Timer, asynchronous file read and write, Asynchronous DNS queries. Powerful functions, which are implemented by a number of distinct processes behind it, describe the division of the next few processes in detail, so that beginners can understand the swoole framework more quickly.

    • Blog: Http://www.cnblogs.com/jhzhu
    • E-mail: jhzhuustc@gmail.com
    • Author: Know so
    • Date: 2015-08-17

Directory

    • [Division of the Php]swoole_server process
      • Directory
      • Swoole Introduction
        • Swoole: Redefining PHP
        • Feature Show Code Snippets
          • TCP Server
          • TCP Client
      • Main process Analysis
        • Master Process
        • Manager process
        • Worker process
        • Task process
      • The correspondence between a process and an event callback
        • Callback functions within the master process
        • Callback functions within a worker process
        • Callback functions within a task process
        • Callback functions within the manager process

Swoole Introduction

Swoole official website

Swoole: Redefining PHP

Swoole:php language's high-performance network communication Framework provides the PHP language for asynchronous multi-threaded servers, asynchronous TCP/UDP network clients, asynchronous MySQL, database connection pooling, Asynctask, Message Queuing, millisecond timers, asynchronous file read and write, asynchronous DNS queries. Swoole, though a standard PHP extension, is actually different from a normal extension. The normal extension simply provides a library function. The swoole extension takes over control of PHP and enters the event loop after it runs. When the IO event occurs, Swoole automatically callbacks the specified PHP function.

Feature Show Code Snippets

TCP Server

$serv = new Swoole_server ("127.0.0.1", 9501); $serv->set (Array (    ' worker_num ' = 8,   //number of worker processes     ' daemonize ' = = true,// Whether as Daemon )); $serv->on (' Connect ', function ($serv, $FD) {    echo "client:connect.\n"; }); $serv->on (' Receive ', function ($serv, $FD, $from _id, $data) {    $serv->send ($FD, ' Swoole: '. $ data);     $serv->close ($FD); }); $serv->on (' Close ', function ($serv, $FD) {    echo "client:close.\n"; }); $serv->start ();

TCP Client

$client = new Swoole_client (swoole_sock_tcp, swoole_sock_async); //Set event callback function $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"; }); //Initiate network connection $client->connect (' 127.0.0.1 ', 9501, 0.5);

For more code snippets, see the Swoole website.

Main process Analysis

Master Process

The master process is primarily used to ensure the operation of the Swoole framework mechanism. It creates several functional threads:

    • Reactor thread: A thread that really handles TCP connections, sending and receiving data. The main thread of the Swoole will assign this connection to a fixed reactor thread after the new connection is held and the thread is responsible for listening to the socket. Reads the data when the socket is readable and parses the protocol to post the request to the worker process. Sends data to a TCP client when the socket is writable.
    • Master threads (main thread): Accept new connections, UNIX proxi signal Processing, timer tasks.
    • Heartbeat Packet Detection Thread: (slightly)
    • UDP packet Thread: (slightly)

Manager process

The worker/task process in Swoole is forked and managed by the manager process.

    • When the child process finishes running, the manager process is responsible for reclaiming the child process and avoiding becoming a zombie process. and create a new child process
    • When the server shuts down, the manager process sends a signal to all child processes to notify the child process to shut down the service
    • When the server reload, the manager process shuts down/restarts the child process individually

Why not the master process, the main reason is that the master process is multi-threaded and cannot safely perform fork operations.

Worker process

    • Accepts request packets posted by the reactor thread and executes the PHP callback function to process the data
    • Generates the response data and sends it to the reactor thread, Cheng to the TCP client by the reactor line
    • Can be asynchronous non-blocking mode or synchronous blocking mode
    • Worker runs in a multi-process manner

Swoole provides a complete process management mechanism when the worker process exits abnormally, such as a fatal error in PHP, being killed by another program, or a normal exit after the number of max_request has been reached. The main process will pull the new worker process back up. The worker process can write code in the same way as a normal apache+php or PHP-FPM. You do not need to write code for asynchronous callbacks like node. js.

Task process

    • Accept tasks that are posted by the worker process through the Swoole_server->task/taskwait method
    • Process the task and return the resulting data to the worker process
    • is completely synchronous blocking mode
    • Task runs in a multi-process manner

The full name of the task process is the task_worker process, which is a special worker process. So onWorkerStart it is also called in the task process. When $worker_id >= $serv->setting['worker_num'] the time indicates that the process is task_worker, otherwise, this process is represented as a worker process.

The correspondence between a process and an event callback

Callback functions within the master process

OnStart OnShutdown Onmasterconnect Onmasterclose OnTimer

Callback functions within a worker process

Onworkerstart Onworkerstop OnConnect OnClose OnReceive OnTimer onfinish

Callback functions within a task process

Ontask Onworkerstart

Callback functions within the manager process

Onmanagerstart Onmanagerstop

The above describes the [php]swoole_server several processes of division of labor, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.