swoole (processes, threads)

process

1, is not shared between processes of any state
2, the scheduling process by the operating system to complete
3, each process has its own separate memory space
4, interprocess communication is mainly achieved by signaling the way, how implementation species, semaphores, pipes, events, etc., any communication efficiency will need a way through the kernel, resulting in communication efficiency is relatively low
5, because it is a separate memory space, context switching when information needs to be saved before the call stack, cpu each register information, virtual memory, and handle the relevant information such as open, resulting in inter-process context switch large overhead communication trouble.

Thread

1, shared between threads variables to solve the problem of access to communication trouble variables need to lock
2, a process can have multiple threads, but each thread will share the parent process as the operating system application resources, including the virtual memory, files, etc., because it is a shared resource, so create a thread required system resources is much smaller than the process, the corresponding number of threads that can be created has become relatively much more.
3, the other in terms of scheduling is due to memory is shared, so when context switches need to save for something like less, so that a context switch becomes effective.

Explanation
  • By php, to run a php file, this time we have created the equivalent of a process, which will reside in the system, the application of its own memory space of system resources and run the appropriate program.
    Here Insert Picture Description
swoole process

Here Insert Picture Description
1, Master process: the main process
2, Manger process: management process
3, Worker process: Working process
4, Task process: asynchronous task worker process

  • The first layer, Master course, this is the main process swoole. This process is used to process core events swoole drive, then in the process of which you can see it has a MainReactor [thread] and several Reactor [thread], swoole all listeners for the event will be realized in these threads, such as connection and signal processing from the client.

  • 1.1, MainReactor (the main thread)
    the main thread is responsible for monitoring server socket, if there is a new connection accept, the main thread will evaluate the number of connections per Reactor thread. This connection assigned to the fewest number of connections reactor thread, do a load balancing.

  • 1.2, Reactor thread group
    Reactor thread is responsible for maintaining the client machine's TCP connection processing network IO, send and receive data completely asynchronous non-blocking mode.
    swoole main thread after the Accept new connection, the connection will be allocated to a fixed thread Reactor read data when read socket, and protocol analysis, process the request delivered to the Worker. Writable in the socket will send data to TCP client.

  • 1.3, heartbeat packet detection thread (HeartbeatCheck)
    after Swoole configured heartbeat detection, the heartbeat packet threads within a fixed time until all the online connection
    transmits detection data packet

  • 1.4, UDP packet receiving threads (UdpRecv)
    receives and processes the client data packet udp

  • swoole want to achieve the best performance you must create multiple worker processes to help deal with the task, but the Worker process must fork operation, but the fork operation is unsafe, if there is no management will be a lot of zombie process, thereby affecting server performance, while the worker process or manslaughter because of the program quit unexpectedly, in order to ensure the stability of the service, you need to re-create the worker process.

  • Swoole in operation will create a separate management process, all the worker processes are processes and task management process from Fork out. Management process monitors all the child process exits, when the worker process to run a fatal error occurs or the end of the life cycle management process will reclaim this process and create a new process. In other words this means that, for the worker, to create a task processes, recycling and other operations solely the "nanny" Manager manage the process

  • worker process belong to the main logic process swoole user handling a series of client requests, received by the Reactor thread posted request packet, and executes the PHP callback processing data generating response data and sent to Reactor thread, transmitted from the Reactor thread to the TCP client can be asynchronous non-blocking mode, it can be synchronous blocking mode

  • This process is asynchronous taskWorker city worker process swoole provided, these processes are mainly used to deal with some lengthy task synchronization, delivered over the worker process them.

client interaction with the server:

1, client request arrives Main Reactor, Client and Master is actually in the process of a Reactor thread connection occurs.

2, Main Reactor Reactor according to circumstances, the register request to the corresponding Reactor (Reactor each has epoll. Listens for change in client)

3, when a client changes the data to the Reactor worker to handle

4, worker process is completed, sent to the corresponding reactor via interprocess communication (such as pipes, shared memory, message queues).

5, reactor in response to the results to the corresponding connection request processing is completed

The callback function in the Master process
  • onStart Server starts in the main process of the main thread callback function
  • onShutdown This event occurs when the normal end Server
Manager callback function in the process
  • onManagerStart called when the management process starts
  • onManagerStop called when the end of the management process
  • When the worker / task_worker process exception occurs after onWorkerError will callback function in this process Manager
The callback function in the Worker process
  • onWorkerStart This event occurs when the Worker Process / Task process starts
  • onWorkerStop This event occurs when the worker process terminates.
  • onConnect have entered a new connection, the callback worker process
  • After onClose TCP client connection is closed, the callback function in worker process
  • OnReceive callback function when the data is received, occurs in the worker process
  • This callback function upon receiving the UDP packet onPacket, occurs in the process worker
  • onFinish when the worker process in the delivery of task completion in task_worker, task process will send the results to the worker tasking process by finish () method.
  • onWorkerExit valid only after opening reload_async characteristics. Asynchronous restart feature
  • When an event is triggered when onPipeMessage work process receives the message sent by pipeline sendMessage
Callback process in Task
  • onTask be called within task_worker process. worker process can use swoole_server_task function to deliver a new task task_worker process
  • onWorkerStart This event occurs when the Worker Process / Task process starts
  • When an event is triggered when onPipeMessage work process receives the message sent by pipeline sendMessage
Brief description:
  • 1, when the server shuts down the program terminates the last time the event was onShutdown.
  • 2, the server started successfully, onStart / onManagerStart / onWorkerStart will execute concurrently in different processes, not sequential.
  • 3. All event callbacks were $ server-> occurred after the start, then start writing the code is invalid code.
  • 4, the order of execution onStart / onManagerStart / onWorkerStart 3 events is uncertain
Run flowchart swoole

Here Insert Picture Description

<?php
//tcp协议
$server=new Swoole\Server("0.0.0.0",9800);   //创建server对象

$server->set([
    'worker_num'=>3, //设置进程
    //'heartbeat_idle_time'=>10,//连接最大的空闲时间
    //'heartbeat_check_interval'=>3 //服务器定时检查
    'open_length_check'=>1,
    'package_length_type'=>'N',//设置包头的长度
    'package_length_offset'=>0, //包长度从哪里开始计算
    'package_body_offset'=>4,  //包体从第几个字节开始计算
    'package_max_length'=>1024 * 1024 * 2,

]);

$server->on("Start",function (){

    var_dump(1);
     //设置主进程的名称
     swoole_set_process_name("server-process:master");
});

//服务关闭时候触发(信号)
$server->on("shutdown",function (){
});


//当管理进程启动时调用它
$server->on('ManagerStart',function (){
    var_dump(2);
    //swoole_set_process_name("server-process:manger");
});

$server->on('WorkerStart',function ($server,$workerId){
   // swoole_set_process_name("server-process:worker");
    var_dump(3);
});

//监听事件,连接事件(woker进程当中)
$server->on('connect',function ($server,$fd){
    echo "新的连接进入:{$fd}".PHP_EOL;
});

//消息发送过来(woker进程当中)
$server->on('receive',function (swoole_server $server, int $fd, int $reactor_id, string $data){
    //var_dump("消息发送过来:".$data);
    //服务端
});

//消息关闭
$server->on('close',function (){
    echo "消息关闭".PHP_EOL;
});
//服务器开启
$server->start();

echo '123456';

Guess you like

Origin blog.csdn.net/qq_33332184/article/details/91473252